Skip to content

Instantly share code, notes, and snippets.

@clausecker
Created April 3, 2018 09:34
Show Gist options
  • Save clausecker/d98bab5d1c87eb89ae8e6badfa1f1e31 to your computer and use it in GitHub Desktop.
Save clausecker/d98bab5d1c87eb89ae8e6badfa1f1e31 to your computer and use it in GitHub Desktop.
section bss
BUFSIZ equ 64
iobuf resb BUFSIZ ; where we put the number
section text
; convert 32 bit number in argument to asciz in iobuf
; return pointer to number in ax
global ltoa@4
ltoa@4: push bp
mov sp,bp
push bx
push di
; high word in [bp+6]
; low word in [bp+4]
mov di,[iobuf+BUFSIZ-1] ; load end of iobuf
mov byte [di],0 ; NUL terminate buffer
.0: mov cx,10 ; divisor
xor dx,dx
mov ax,[bp+6] ; load high word
div cx ; pre-divide high word
mov bx,ax ; bx: new high word
mov ax,[bp+4] ; load low word
div cx ; actual division
mov [bp+4],ax ; store new low word
mov [bp+6],bx ; store new high word
add dl,'0' ; convert remainder to ASCII
dec di ; make space in output buffer
mov [di],dl ; write digit to output buffer
test bx,bx ; are we done yet (high word)?
jnz .0
test ax,ax ; are we done yet (low word)?
jnz .0
mov ax,di ; return beginning of buffer
pop di
pop bx
pop bp
ret 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment