libstd/string.68k
2024-03-19 09:24:25 -05:00

72 lines
1.3 KiB
Plaintext

section .text,text
public hex_to_ascii
; Writes the hexdecimal ASCII representation of d0.l to the 11-byte buffer at a0
hex_to_ascii:
movem.l d2/d3, -(a7) ; Save old values of d2 and d3 (callee preserved)
move.b #'0', (a0)+
move.b #'x', (a0)+
move.b #0, d1
move.w #28, d2
hex_to_ascii_loop:
cmpi.w #0, d2
beq.b hex_to_ascii_loop_done
move.l d0, d3
lsr.l d2, d3
andi.l #$F, d3
cmp.l #0, d3
bne.b .1
cmp.b #0, d1
bne.b .1
subi.w #4, d2
bra.b hex_to_ascii_loop
.1:
move.b #1, d1
cmp.b #$A, d3
blt.b .2
add.b #('a' - $A), d3
move.b d3, (a0)+
bra.b .3
.2:
add.b #'0', d3
move.b d3, (a0)+
.3:
subi.w #4, d2
bra.b hex_to_ascii_loop
hex_to_ascii_loop_done:
move.l d0, d3
andi.l #$F, d3
cmp.b #$A, d3
blt.b .2
add.b #('a' - $A), d3
move.b d3, (a0)+
bra.b .3
.2:
add.b #'0', d3
move.b d3, (a0)+
.3:
move.b #0, (a0)
movem.l (a7)+, d2/d3 ; Restore d2 and d3 (callee preserved)
rts
; Compares the strings in a0 and a1
; Returns 0 in d0.b if identical
; Returns difference of first mismatching character in d0.b (s1-s2) if different
public strcmp
strcmp:
strcmp_loop:
move.b (a0), d0
cmp.b (a1), d0
bne.b strcmp_loop_done
cmpi.b #0, (a0)
bne.b .1
move.b #0, d0
rts
.1:
adda.l #1, a0
adda.l #1, a1
bra.b strcmp_loop
strcmp_loop_done:
move.b (a0), d0
sub.b (a1), d0
rts