make print_dec clearer

This commit is contained in:
Lucia Ceionia 2023-12-02 21:35:17 -06:00
parent 563076a899
commit ba2250c29c
2 changed files with 20 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.o
a.out
solution

View File

@ -123,34 +123,42 @@ pop ecx
pop ebx
ret
; i hate this
; input in EAX, all regs unmodified
print_dec:
pushad ; save regs
; max 4294967296 is 10 chars
; round to nearest 32-bit boundary
sub esp, 12
lea ecx, [esp+11]
; string in ECX, length in EDX
lea ecx, [esp+11] ; last possible byte
; check for 0
test eax, eax
jz .zero
mov esi, 0
mov ebx, 10
mov ebx, 10 ; base 10
xor esi, esi ; counter
.div_shit:
xor edx, edx
; divide
div ebx
dec ecx
dec ecx ; next char
inc esi
; store
add dl, '0'
mov byte [ecx], dl
; check if done
test eax, eax
jnz .div_shit
mov edx, esi
jnz .div_shit ; continue
mov edx, esi ; counter in edx
jmp .write
.zero:
mov byte [esp+10], 0
lea ecx, [esp+10]
mov edx, 1 ; count
mov byte [ecx], '0'
mov edx, 1 ; length
.write:
mov eax, 4 ; write
mov ebx, 1 ; stdout
int 0x80
add esp, 12
add esp, 12 ; restore stack
popad ; restore regs
ret
[section .data]