-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinting_numbers.asm
More file actions
75 lines (63 loc) · 1.43 KB
/
Copy pathprinting_numbers.asm
File metadata and controls
75 lines (63 loc) · 1.43 KB
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
74
75
section .bss
num resb 32
num_len resd 2
temp resb 40
section .data
msg db "Printing numbers ",0xA
len equ $-msg
new_l db 0xD,0xA
new_l_len equ $-new_l
lim db 45
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,len
int 0x80
mov eax,1
mov [temp],eax
ll_1:
mov edi, num ; Argument: Address of the target string
call int2str ; Get the digits of EAX and store it as ASCII
sub edi, num ; EDI (pointer to the terminating NULL) - pointer to sum = length of the string
mov [num_len], edi
mov eax,4
mov ebx,1
mov ecx,num
mov edx,[num_len]
int 0x80
mov eax,4
mov ebx,1
mov ecx,new_l
mov edx,new_l_len
int 0x80
mov eax,[temp]
inc eax
mov [temp],eax
cmp eax,[lim]
jg _exit
jmp ll_1
_exit:
mov eax, 1
mov ebx, 0
int 0x80
int2str: ; Converts an positive integer in EAX to a string pointed to by EDI
xor ecx, ecx
mov ebx, 10
.LL1: ; First loop: Save the remainders
xor edx, edx ; Clear EDX for div
div ebx ; EDX:EAX/EBX -> EAX Remainder EDX
push dx ; Save remainder
inc ecx ; Increment push counter
test eax, eax ; Anything left to divide?
jnz .LL1 ; Yes: loop once more
.LL2: ; Second loop: Retrieve the remainders
pop dx ; In DL is the value
or dl, '0' ; To ASCII
mov [edi], dl ; Save it to the string
inc edi ; Increment the pointer to the string
loop .LL2 ; Loop ECX times
mov byte [edi], 0 ; Termination character
ret ; RET: EDI points to the terminating NULL