-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfd_printnum.asm
73 lines (69 loc) · 1.58 KB
/
fd_printnum.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
; prints the number held by eax, to the fd held by ebx
; in case of error, returns a value less or equal to 0 on eax
; otherwise returns 1
%define write 4
section .text
global fd_printnum
fd_printnum:
push ebp
mov ebp, esp
sub esp, 16 ; 3 ints, 1 byte to check if I should print, 2 useless and one to store char c
mov [esp], eax ; num
mov [esp+4], dword 1000000000 ; max significant 32bit int
mov [esp+8], ebx ; fd
cmp eax, 0
jge _find_first_digit
mov [esp + 12], dword '-'
add eax, dword 1 ; takes care of int_min case
imul eax, dword -1
inc eax
mov [esp], eax
jmp _write_digit
_find_first_digit:
mov eax, dword [esp]
mov ecx, dword [esp+4]
cmp ecx, dword 1
jle _get_next_digit ; in case number is 1 digit
xor edx, edx
div ecx
cmp eax, 0
jg _get_next_digit ; ready to print
mov eax, ecx
xor edx, edx
mov ecx, 10
div ecx
mov [esp+4], eax ;next divisor
jmp _find_first_digit
_get_next_digit:
mov eax, dword [esp]
mov ecx, dword [esp+4]
cmp ecx, dword 0
jle _success
xor edx, edx
div ecx
mov [esp], edx ; remainder
add eax, dword '0'
mov [esp+12], eax
mov eax, ecx
xor edx, edx
mov ecx, 10
div ecx
mov [esp+4], eax ; next divider
_write_digit:
mov eax, write
mov ebx, dword [esp+8]
lea ecx, [esp+12]
mov edx, dword 1
int 0x80
cmp eax, 0
jle _ret
mov ebx, dword [esp+12]
cmp ebx, '-'
je _find_first_digit
jmp _get_next_digit
_success:
mov eax, dword 1
_ret:
mov esp, ebp
pop ebp
ret