make print_dec clearer
This commit is contained in:
parent
563076a899
commit
ba2250c29c
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
*.o
|
*.o
|
||||||
a.out
|
a.out
|
||||||
|
solution
|
||||||
|
30
02/main.s
30
02/main.s
@ -123,34 +123,42 @@ pop ecx
|
|||||||
pop ebx
|
pop ebx
|
||||||
ret
|
ret
|
||||||
|
|
||||||
; i hate this
|
; input in EAX, all regs unmodified
|
||||||
print_dec:
|
print_dec:
|
||||||
|
pushad ; save regs
|
||||||
|
; max 4294967296 is 10 chars
|
||||||
|
; round to nearest 32-bit boundary
|
||||||
sub esp, 12
|
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
|
test eax, eax
|
||||||
jz .zero
|
jz .zero
|
||||||
mov esi, 0
|
mov ebx, 10 ; base 10
|
||||||
mov ebx, 10
|
xor esi, esi ; counter
|
||||||
.div_shit:
|
.div_shit:
|
||||||
xor edx, edx
|
xor edx, edx
|
||||||
|
; divide
|
||||||
div ebx
|
div ebx
|
||||||
dec ecx
|
dec ecx ; next char
|
||||||
inc esi
|
inc esi
|
||||||
|
; store
|
||||||
add dl, '0'
|
add dl, '0'
|
||||||
mov byte [ecx], dl
|
mov byte [ecx], dl
|
||||||
|
; check if done
|
||||||
test eax, eax
|
test eax, eax
|
||||||
jnz .div_shit
|
jnz .div_shit ; continue
|
||||||
mov edx, esi
|
mov edx, esi ; counter in edx
|
||||||
jmp .write
|
jmp .write
|
||||||
.zero:
|
.zero:
|
||||||
mov byte [esp+10], 0
|
mov byte [ecx], '0'
|
||||||
lea ecx, [esp+10]
|
mov edx, 1 ; length
|
||||||
mov edx, 1 ; count
|
|
||||||
.write:
|
.write:
|
||||||
mov eax, 4 ; write
|
mov eax, 4 ; write
|
||||||
mov ebx, 1 ; stdout
|
mov ebx, 1 ; stdout
|
||||||
int 0x80
|
int 0x80
|
||||||
add esp, 12
|
add esp, 12 ; restore stack
|
||||||
|
popad ; restore regs
|
||||||
ret
|
ret
|
||||||
|
|
||||||
[section .data]
|
[section .data]
|
||||||
|
Loading…
Reference in New Issue
Block a user