Skip to content

Commit fd3e09a

Browse files
committed
add print_char and a todo
1 parent f57cdc0 commit fd3e09a

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
[x86 assembly language](https://en.wikipedia.org/wiki/X86_assembly_language)
88

9+
[What is stack frame](https://stackoverflow.com/questions/3699283/what-is-stack-frame-in-assembly)
10+
911
```
1012
/usr/include/asm-generic/fcntl.h
1113
```

print_int/.gdbinit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ lay asm
33
define hook-quit
44
set confirm off
55
end
6-
break le_print
6+
break print_int_pop_loop

print_int/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ run: build
66
./print_int
77

88
check: build
9-
./print_int | md5sum | grep -q ^823c843e5 && printf "\033[1;32m[OK]\033[0m\n"
9+
./print_int | md5sum | grep -q ^0d0f199 && printf "\033[1;32m[OK]\033[0m\n"
1010

1111
clean:
1212
rm -f print_int

print_int/print_int.s

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@
99
.text
1010
.globl main
1111

12+
# char rdi
13+
print_char:
14+
push %rbp
15+
mov %rsp, %rbp
16+
push %rax
17+
push %rcx
18+
push %rdx
19+
push %rsi
20+
push %rdi
21+
mov $WRITE, %rax
22+
mov %rdi, -128(%rbp)
23+
lea -128(%rbp), %rsi
24+
mov $STDOUT, %rdi
25+
mov $1, %rdx
26+
syscall
27+
pop %rdi
28+
pop %rsi
29+
pop %rdx
30+
pop %rcx
31+
pop %rax
32+
mov %rbp, %rsp
33+
pop %rbp
34+
ret
35+
1236
# char *rsi, int rdx
1337
write_string:
1438
mov $WRITE, %rax
@@ -23,37 +47,54 @@ print_int:
2347
push %rsi
2448
push %rdx
2549
push %rcx
50+
push %rdi
2651
mov %rsp, %rbp # save stack pointer
52+
2753
mov $0, %rcx
54+
2855
print_int_push_loop:
2956
mov %rsi, %rax
30-
and $15, %rax
31-
add $48, %rax # %rax now contains ascii "0"-"9"
32-
cmp $57, %rax
57+
and $0xf, %rax
58+
add $0x30, %rax # %rax now contains ascii "0"-"9"
59+
cmp $0x39, %rax
3360
jle print_int_after_adjust
3461
add $39, %rax # adjust for ascii "a"-"f"
3562
print_int_after_adjust:
63+
#movb %al, -64(%rbp, %rcx) #to do: use this instead of push
3664
push %rax
3765
inc %rcx
3866
shr $4, %rsi
3967
test %rsi, %rsi
4068
jnz print_int_push_loop
69+
70+
add $2, %rcx
71+
4172
mov $0, %rdx
73+
4274
movb $0x30, -128(%rbp, %rdx)
4375
inc %rdx
76+
4477
movb $0x78, -128(%rbp, %rdx)
4578
inc %rdx
79+
4680
print_int_pop_loop: # copy chars from stack
4781
pop %rax
82+
83+
mov %rax, %rdi
84+
#call print_char
85+
4886
movb %al, -128(%rbp, %rdx)
4987
inc %rdx
50-
cmp %rsp, %rbp
88+
cmp %rdx, %rcx
5189
jne print_int_pop_loop
90+
5291
movb $0xa, -128(%rbp, %rdx)
5392
inc %rdx
93+
5494
lea -128(%rbp), %rsi
5595
call write_string
5696
mov %rbp, %rsp # restore stack pointer
97+
pop %rdi
5798
pop %rcx
5899
pop %rdx
59100
pop %rsi
@@ -62,9 +103,9 @@ print_int_pop_loop: # copy chars from stack
62103
ret
63104

64105
main:
65-
mov $0xdead, %rsi
106+
mov $0x9084, %rsi
66107
shl $16, %rsi
67-
add $0xbeef, %rsi
108+
add $0xa412, %rsi
68109
call print_int
69110
jmp exit
70111

0 commit comments

Comments
 (0)