Jump to content

Increment a numerical string: Difference between revisions

Line 3,370:
 
=={{header|Z80 Assembly}}==
As a test case, we'll start with 1999 and increment it to 2000.
It's much easier to do this using binary-coded decimal thanks to the <code>DAA</code> instruction.
<lang z80>;;;;;;;;;;;;;;;;;;; HEADER ;;;;;;;;;;;;;;;;;;;
read "\SrcCPC\winape_macros.asm"
Line 3,378:
 
org &1000
 
 
ld hl,NumericString
 
Line 3,387 ⟶ 3,385:
jr z,displaystring
add &01
cp &3A
daa
jr ncnz,displaystring
ld (hl),a
jr nc,displaystring
;carry forward
ld a,&30
ld (hl),a
inc hl
jr incstring
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
displaystring:
ld (hl),a ;store the last addition.
ld hl,NumericString_End
dec hl
Line 3,401:
cp 255
ret z
call showhex&bb5a
dec hl
jr disploop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
db 255 ;terminator when reading in reverse
NumericString:
;stored little-endian for convenience
db &9939,&9939,&9939,&0131
NumericString_End:
db 255 ;terminator
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
read "\SrcCPC\winape_showhex.asm"
read "\SrcCPC\winape_stringop.asm"</lang>
 
{{out}}
<pre>020000002000</pre>
 
=={{header|zkl}}==
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.