Difference between revisions of "Z80 Routines:Other:DispA"

From WikiTI
Jump to: navigation, search
m (category)
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
[[Category:Z80 Routines:Display|DispA]]
 
[[Category:Z80 Routines:Other|DispA]]
 
[[Category:Z80 Routines:Other|DispA]]
 
[[Category:Z80 Routines|DispA]]
 
[[Category:Z80 Routines|DispA]]
  
== Unsigned Version ==
+
Simple and small routines that print the value of z80 register a.
  
<nowiki>;NOT TESTED. I WILL TEST LATER. SORRY THE CAPS; I HATE THEM; TOO.
+
== Decimal Unsigned Version ==
;Example: display a=56 as "056".
+
DispA:
+
ld de,OP1 ; safe ram to store the ASCII string. Use other one in case you do floating point operations with b_calls
+
push de ;we will need it because the output is after the string
+
call AtoDec
+
ex de,hl
+
ld (hl),$00
+
pop hl
+
b_call(_PutS)
+
ret
+
  
;Number in a to decimal ASCII
+
<nowiki>;Number in a to decimal ASCII
 
;adapted from 16 bit found in z80 Bits to 8 bit by Galandros
 
;adapted from 16 bit found in z80 Bits to 8 bit by Galandros
;input: a = number, (de) = decimal ASCII
+
;Example: display a=56 as "056"
;destroys af,bc
+
;input: a = number
AtoDec:
+
;Output: a=0,value of a in the screen
 +
;destroys af,bc (don't know about hl and de)
 +
DispA:
 
ld c,-100
 
ld c,-100
 
call Na1
 
call Na1
Line 30: Line 23:
 
add a,c
 
add a,c
 
jr c,Na2
 
jr c,Na2
sub c ;works as add 100/10/1
+
sub c ;works as add 100/10/1
ld c,a ;save a
+
push af ;safer than ld c,a
ld a,b ;ld (de),b is not a instruction
+
ld a,b ;char is in b
ld (de),a
+
CALL PUTCHAR ;plot a char. Replace with bcall(_PutC) or similar.
ld a,c ;restore a
+
pop af ;safer than ld a,c
inc de
+
ret
ret</nowiki>
+
</nowiki>
 +
 
 +
== Hexadecimal Unsigned Version ==
 +
 
 +
<nowiki>;Soon...
 +
</nowiki>
  
== Signed Version ==
+
== Decimal Signed Version ==
 
  <nowiki>
 
  <nowiki>
 
; DispA
 
; DispA

Latest revision as of 05:51, 21 April 2010


Simple and small routines that print the value of z80 register a.

Decimal Unsigned Version

;Number in a to decimal ASCII
;adapted from 16 bit found in z80 Bits to 8 bit by Galandros
;Example: display a=56 as "056"
;input: a = number
;Output: a=0,value of a in the screen
;destroys af,bc (don't know about hl and de)
DispA:
	ld	c,-100
	call	Na1
	ld	c,-10
	call	Na1
	ld	c,-1
Na1:	ld	b,'0'-1
Na2:	inc	b
	add	a,c
	jr	c,Na2
	sub	c		;works as add 100/10/1
	push af		;safer than ld c,a
	ld	a,b		;char is in b
	CALL	PUTCHAR	;plot a char. Replace with bcall(_PutC) or similar.
	pop af		;safer than ld a,c
	ret

Hexadecimal Unsigned Version

;Soon...

Decimal Signed Version

; DispA
; --------------------------------------------------------------
; Converts a signed integer value to a zero-terminated ASCII
; string representative of that value (using radix 10).
; --------------------------------------------------------------
; INPUTS:
;     HL     Value to convert (two's complement integer).
;     DE     Base address of string destination. (pointer).
; --------------------------------------------------------------
; OUTPUTS:
;     None
; --------------------------------------------------------------
; REGISTERS/MEMORY DESTROYED
; AF HL
; --------------------------------------------------------------

DispA:
   push    de
   push    bc

; Detect sign of HL.
    bit    7, h
    jr     z, _DoConvert

; HL is negative. Output '-' to string and negate HL.
    ld     a, '-'
    ld     (de), a
    inc    de

; Negate HL (using two's complement)
    xor    a
    sub    l
    ld     l, a
    ld     a, 0     ; Note that XOR A or SUB A would disturb CF
    sbc    a, h
    ld     h, a

; Convert HL to digit characters
_DoConvert:
    ld     b, 0     ; B will count character length of number
-   ld     a, 10
    syscall  DivHLByA  ; HL = HL / A, A = remainder
    push   af
    inc    b
    ld     a, h
    or     l
    jr     nz, -

; Retrieve digits from stack
-   pop    af
    or     $30
    ld     (de), a
    inc    de
    djnz   -

; Terminate string with NULL
    xor    a
    ld     (de), a

    pop    bc
    pop    de
    ret

Comments

  • This routine uses the common positive-integer radix conversion algorithm of repeatedly dividing a value by the desired radix (base case: the value is zero) and saving the remainders of each division.
  • As this algorithm gives the digits in reverse order, they are pushed onto the hardware stack so that subsequently popping them yields the correct order.
  • Note that the output string will not be fixed-width.

Example Usage

    ld    hl, -1004
    ld    de, OP1
    call  DispA
    ld    hl, OP1
    syscall  PutS