Skip to content

Commit 2b00b92

Browse files
committed
print_int: add memcpy
1 parent e2e8f3c commit 2b00b92

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

group3/group3.s

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

170170
add %rax, %rbx
171171

172+
write linebuf(%rip), $3
173+
writeln
172174
write linebuf(%rip), %rax
173175
writeln
174176
plop

print_int/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/print_int
2+
/write

print_int/Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ build:
22
$(CC) main.s -c -o print_int.o
33
$(LD) -e main -o print_int print_int.o
44

5-
run: build
6-
./print_int
7-
85
check: build
96
./print_int | md5sum | grep -q ^3cca2fa && printf "\033[1;32m[OK]\033[0m\n"
107

8+
build_write:
9+
$(CC) write.s -c -o write.o
10+
$(LD) -e main -o write write.o
11+
12+
check_write: build_write
13+
./write | grep -q '^ABC$$' && printf "\033[1;32m[OK]\033[0m\n"
14+
15+
1116
clean:
12-
rm -f print_int
17+
rm -f *.o write print_int

print_int/print_int.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
add $8, %rsp
3030
.endm
3131

32+
.macro memcpy source dest count
33+
push \source
34+
push \dest
35+
push \count
36+
call copymem
37+
add $24, %rsp
38+
.endm
39+
3240
.macro write address size
3341
push \address
3442
push \size
@@ -105,20 +113,24 @@ write_char:
105113
write_newline:
106114
enter
107115
push_all
116+
push %rax
108117
push $0xa
109118
call write_char
110119
plop
120+
pop %rax
111121
pop_all
112122
return
113123

114124
write_string:
115125
enter
116126
push_all
127+
push %rax
117128
mov 24(%rbp), %rsi # param: address
118129
mov 16(%rbp), %rdx # param: size
119130
mov $WRITE, %rax
120131
mov $STDOUT, %rdi
121132
syscall
133+
pop %rax
122134
pop_all
123135
return
124136

@@ -175,3 +187,23 @@ print_int_pop_loop:
175187
pop_all
176188
return
177189

190+
copymem:
191+
enter
192+
push_all
193+
push %rax
194+
mov 32(%rbp), %rsi # param: source
195+
mov 24(%rbp), %rdi # param: destination
196+
mov 16(%rbp), %rdx # param: count
197+
mov $0, %rcx
198+
test %rdx, %rdx
199+
jz copymem_done
200+
copymem_loop:
201+
mov (%rsi, %rcx), %rax
202+
movb %al, (%rdi, %rcx)
203+
inc %rcx
204+
cmp %rcx, %rdx
205+
jne copymem_loop
206+
copymem_done:
207+
pop %rax
208+
pop_all
209+
return

print_int/write.s

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
.data
2+
foo:
3+
.ascii "abc"
4+
.text
5+
.globl main
6+
7+
.include "print_int.s"
8+
9+
main:
10+
enter
11+
sub $48, %rsp
12+
movb $0x41, -24(%rbp)
13+
movb $0x42, -23(%rbp)
14+
movb $0x43, -22(%rbp)
15+
movb $0xa, -21(%rbp)
16+
mov %rbp, %rsi
17+
sub $24, %rsi
18+
mov %rsi, %rax
19+
sub $24, %rax
20+
memcpy %rsi, %rax, $4
21+
write %rax, $4
22+
call exit
23+
24+
exit:
25+
movq $EXIT, %rax
26+
movq $0, %rdi
27+
syscall

0 commit comments

Comments
 (0)