-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPrint_Integer.asm
52 lines (41 loc) · 1.68 KB
/
Print_Integer.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
section .data
digit db 0
section .text
global _start
_start:
mov rax, 1234 ; store 1234 in rax, the integer to print
call _printInteger ; call subroutine '_printInteger'
; perform sys_exit
mov rax, 60
mov rdi, 0
syscall
; prints integer in rax
_printInteger:
mov rcx, digit ; start at beginning of integer
mov rbx, 10 ; move newline into rbx
mov [rcx], rbx ; move newline into rcx
inc rcx ; increment rcx (position)
; loop to store value of integer
_loopStore:
xor rdx, rdx ; zero-out rdx register
mov rbx, 10 ; (divide rbx
div rbx ; by 10)
add rdx, 48 ; rdx is the remainder of rax / rbx, and add 48 to give its ASCII value
mov [rcx], dl ; load the character of rdx
inc rcx ; increment position
cmp rax, 0 ; (continue loop until
jne _loopStore ; rax becomes 0)
; loop to print string backwards
_loopPrint:
push rcx ; push rcx into stack
; perform sys_write
mov rax, 1
mov rdi, 1
mov rsi, rcx
mov rdx, 1
syscall
pop rcx ; get back position
dec rcx ; decrement position
cmp rcx, digit ; (continue loop until
jge _loopPrint ; end of string)
ret