Skip to content

Commit f0ec06a

Browse files
author
Margo Seltzer
committed
Add exercises for Thursday 9/22
1 parent c2c701c commit f0ec06a

20 files changed

+530
-0
lines changed

asm2x/2a.S

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.file "2a.c"
2+
.section .rodata.str1.1,"aMS",@progbits,1
3+
.LC0:
4+
.string <Format String removed>
5+
.text
6+
.globl output_func
7+
.type output_func, @function
8+
output_func:
9+
.LFB23:
10+
subq $8, %rsp
11+
movslq %esi, %rsi
12+
movl (%rdi,%rsi,4), %edx
13+
movl $.LC0, %esi
14+
movl $1, %edi
15+
movl $0, %eax
16+
call __printf_chk
17+
addq $8, %rsp
18+
ret
19+
.LFE23:
20+
.size output_func, .-output_func
21+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
22+
.section .note.GNU-stack,"",@progbits

asm2x/2b.S

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.file "2b.c"
2+
.section .rodata.str1.1,"aMS",@progbits,1
3+
.LC0:
4+
.string <Format String removed>
5+
.text
6+
.globl output_func
7+
.type output_func, @function
8+
output_func:
9+
.LFB23:
10+
subq $8, %rsp
11+
movslq %esi, %rsi
12+
movsbl (%rdi,%rsi), %edx
13+
movl $.LC0, %esi
14+
movl $1, %edi
15+
movl $0, %eax
16+
call __printf_chk
17+
addq $8, %rsp
18+
ret
19+
.LFE23:
20+
.size output_func, .-output_func
21+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
22+
.section .note.GNU-stack,"",@progbits

asm2x/2c.S

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
.file "2c.c"
2+
.section .rodata.str1.1,"aMS",@progbits,1
3+
.LC0:
4+
.string <Format String removed>
5+
.text
6+
.globl output_func
7+
.type output_func, @function
8+
output_func:
9+
.LFB23:
10+
subq $8, %rsp
11+
movslq %esi, %rsi
12+
movq (%rdi,%rsi,8), %rdx
13+
movl $.LC0, %esi
14+
movl $1, %edi
15+
movl $0, %eax
16+
call __printf_chk
17+
addq $8, %rsp
18+
ret
19+
.LFE23:
20+
.size output_func, .-output_func
21+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
22+
.section .note.GNU-stack,"",@progbits

asm2x/2tmplt.c

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// FILL IN:
2+
// TYPE -- with the type you think the argument should take.
3+
// COMBINATION-OF-ARG1-and-ARG2 -- some way combining the two arguments to
4+
// produce the assembly you want.
5+
// SOMEFORMAT -- a format that will let you print the value of the
6+
// combination of arg1 and arg2
7+
#include <stdio.h>
8+
void output_func(TYPE arg1, int arg2) {
9+
printf("%SOMEFORMAT\n", COMBINATION-OF-ARG1-and-ARG2);
10+
}

asm2x/3a.S

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
.file "3a.c"
2+
.section .rodata.str1.1,"aMS",@progbits,1
3+
.LC0:
4+
.string <Format String removed>
5+
.LC1:
6+
.string <Format String removed>
7+
.LC2:
8+
.string <Format String removed>
9+
.text
10+
.globl output_func
11+
.type output_func, @function
12+
output_func:
13+
.LFB23:
14+
pushq %rbx
15+
movq %rdi, %rbx
16+
movl (%rdi), %edx
17+
movl $.LC0, %esi
18+
movl $1, %edi
19+
movl $0, %eax
20+
call __printf_chk
21+
movswl 4(%rbx), %edx
22+
movl $.LC1, %esi
23+
movl $1, %edi
24+
movl $0, %eax
25+
call __printf_chk
26+
movsbl 6(%rbx), %edx
27+
movl $.LC2, %esi
28+
movl $1, %edi
29+
movl $0, %eax
30+
call __printf_chk
31+
popq %rbx
32+
ret
33+
.LFE23:
34+
.size output_func, .-output_func
35+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
36+
.section .note.GNU-stack,"",@progbits

asm2x/3b.S

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.file "3b.c"
2+
.section .rodata.str1.1,"aMS",@progbits,1
3+
.LC0:
4+
.string <Format String removed>
5+
.LC1:
6+
.string <Format String removed>
7+
.text
8+
.globl output_func
9+
.type output_func, @function
10+
output_func:
11+
.LFB23:
12+
pushq %rbx
13+
movq %rdi, %rbx
14+
call puts
15+
movq 24(%rbx), %rdx
16+
movl $.LC0, %esi
17+
movl $1, %edi
18+
movl $0, %eax
19+
call __printf_chk
20+
movswl 32(%rbx), %edx
21+
movl $.LC1, %esi
22+
movl $1, %edi
23+
movl $0, %eax
24+
call __printf_chk
25+
popq %rbx
26+
ret
27+
.LFE23:
28+
.size output_func, .-output_func
29+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
30+
.section .note.GNU-stack,"",@progbits

asm2x/3tmplt.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// FILL IN:
2+
// STRUCT: Complete the struct declaration below, so that that when this file
3+
// is compiled, it produces the assembly code found in file 3a.S and 3b.S
4+
// FORMAT[123]: Use the correct printf format string to print out the
5+
// relevant fields of the structure.
6+
#include <stdio.h>
7+
struct x {
8+
STRUCT
9+
};
10+
11+
void output_func(struct x *arg1) {
12+
printf("FORMAT1\n", arg1->f1);
13+
printf("FORMAT2\n", arg1->f2);
14+
printf("FORMAT3\n", arg1->f3);
15+
}

asm2x/4a.S

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.file "4a.c"
2+
.section .rodata.str1.1,"aMS",@progbits,1
3+
.LC0:
4+
.string <Format string removed>
5+
.LC1:
6+
.string <Format string removed>
7+
.text
8+
.globl output_func
9+
.type output_func, @function
10+
output_func:
11+
.LFB23:
12+
pushq %rbx
13+
movq %rdi, %rbx
14+
call puts
15+
movl (%rbx), %edx
16+
movl $.LC0, %esi
17+
movl $1, %edi
18+
movl $0, %eax
19+
call __printf_chk
20+
movq (%rbx), %rdx
21+
movl $.LC1, %esi
22+
movl $1, %edi
23+
movl $0, %eax
24+
call __printf_chk
25+
popq %rbx
26+
ret
27+
.LFE23:
28+
.size output_func, .-output_func
29+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
30+
.section .note.GNU-stack,"",@progbits

asm2x/4tmplt.c

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// FILL IN:
2+
// UNION: Complete the union declaration below, so that that when this file
3+
// is compiled, it produces the assembly code found in file 4a.S and 4b.S
4+
// FORMAT[123]: Use the correct printf format string to print out the
5+
// relevant fields of the structure.
6+
#include <stdio.h>
7+
union x {
8+
UNION
9+
};
10+
11+
void output_func(struct x *arg1) {
12+
printf("FORMAT1\n", arg1->u1);
13+
printf("FORMAT2\n", arg1->u2);
14+
printf("FORMAT3\n", arg1->u3);
15+
}

asm2x/GNUmakefile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
ASMS := $(patsubst %.c,%.s,$(wildcard *.c))
2+
ASM_OBJECTS := $(patsubst %.s,%.o,$(ASMS))
3+
ASM_PROGRAMS := $(shell grep -l main $(wildcard *.[cs]) | sed 's,\.[cs],,' | sort -u)
4+
DEFAULT_ASM_CFLAGS = -O1
5+
DEFAULT_ASM_CFLAGS_DEBUG = -g $(DEFAULT_ASM_CFLAGS)
6+
7+
PROGRAMS = $(ASM_PROGRAMS)
8+
9+
all: $(ASMS) $(PROGRAMS)
10+
11+
ALLPROGRAMS = $(PROGRAMS)
12+
PREFER_GCC ?= 1
13+
14+
include ../common/rules.mk
15+
16+
%.o: %.c $(BUILDSTAMP)
17+
$(call run,$(CC) $(CFLAGS) -O1 $(DEPCFLAGS) -o $@ -c,COMPILE,$<)
18+
19+
$(ASM_OBJECTS): %.o: %.s $(BUILDSTAMP)
20+
$(call run,$(CC) -o $@ -c,ASSEMBLE,$<)
21+
22+
%.noopt.o: %.c $(BUILDSTAMP)
23+
$(call run,$(CC) $(CFLAGS) -O0 $(DEPCFLAGS) -o $@ -c,COMPILE -O0,$<)
24+
25+
%.prof.o: %.c $(BUILDSTAMP)
26+
$(call run,$(CC) $(CFLAGS) -pg -O1 $(DEPCFLAGS) -o $@ -c,COMPILE -pg,$<)
27+
28+
29+
%.s: %.c GNUmakefile
30+
@ARGS=$$(grep '^//!' $< | sed 's/.*! *//'); \
31+
CFLAGS="`echo "$(CFLAGS)" | sed 's/ *-g */ /'`"; \
32+
if test -z "$$ARGS"; then ARGS="$(DEFAULT_ASM_CFLAGS)"; fi; \
33+
$(call xrun,$(CC) $$CFLAGS $$ARGS -o $@ -S $<,COMPILE -S $$ARGS $<) && { $(call cleanasm,$@); }
34+
35+
$(ASM_PROGRAMS): %: %.s
36+
@ARGS=$$(grep '^//!' $< | sed 's/.*! *//'); \
37+
CFLAGS="`echo "$(CFLAGS)" | sed 's/ *-g */ /'`"; \
38+
if test -z "$$ARGS"; then ARGS="$(DEFAULT_ASM_CFLAGS)"; fi; \
39+
$(call xrun,$(CC) $$CFLAGS $$ARGS -o $@ $<,ASSEMBLE $@)
40+
41+
42+
clean:
43+
$(call run,rm -f $(ALLPROGRAMS) *.o *.dSYM *.core,CLEAN)
44+
$(call run,rm -rf $(DEPSDIR))
45+
$(call run,for i in $(ASMS); do x=`basename -s .s $$i`; if grep main $$i $$x.c >/dev/null 2>&1; then rm -f $$x; fi; if test -r $$x.c; then rm -f $$i; fi; done)
46+
47+
insert-f%:
48+
../common/renumber.pl -i f$*
49+
delete-f%:
50+
../common/renumber.pl -d f$*
51+
52+
.PHONY: all clean insert-f% delete-f%
53+

asm2x/README

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
In-class exercise to accompany introduction of addressing modes in
2+
assembly language.
3+
4+
We provide snippets of assembly that manipulate various fundamental and
5+
compound types in assembly language and ask students to write the C code
6+
to generate the given assembly.

asm2x/for.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void dofor(int a, int b) {
5+
for (int i = a; i < b; i++)
6+
printf("x");
7+
printf("\n");
8+
}
9+
10+
int main(int argc, char* argv[]) {
11+
if (argc <= 2) {
12+
fprintf(stderr, "Usage: for A B\nA must be less than B\n");
13+
exit(1);
14+
}
15+
16+
int a = strtol(argv[1], 0, 0);
17+
int b = strtol(argv[2], 0, 0);
18+
if (a >= b) {
19+
fprintf(stderr, "Usage: for A B\nA must be less than B\n");
20+
exit(1);
21+
}
22+
dofor(a, b);
23+
}

asm2x/if.c

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void doif(int a, int b) {
5+
if (a == b)
6+
printf("Equal!\n");
7+
else
8+
printf("Not equal!\n");
9+
}
10+
11+
int main(int argc, char* argv[]) {
12+
if (argc <= 2) {
13+
fprintf(stderr, "Usage: if A B\n");
14+
exit(1);
15+
}
16+
17+
int a = strtol(argv[1], 0, 0);
18+
int b = strtol(argv[2], 0, 0);
19+
doif(a, b);
20+
}

asm2x/mystery-template.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// All the mystery functions are of the form:
2+
int mysteryN(SOMETIME arg, int key) {
3+
// Returns 1 if key appears in data structure arg
4+
// Returns 0 if key does not appear in data structure arg
5+
}

asm2x/mystery1.S

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
.file "mystery1.c"
2+
.text
3+
.globl mystery
4+
.type mystery, @function
5+
mystery:
6+
.LFB0:
7+
leaq 4(%rdi), %r8
8+
movl (%rdi), %edx
9+
subl $1, %edx
10+
js .L7
11+
movl %edx, %eax
12+
shrl $31, %eax
13+
addl %edx, %eax
14+
sarl %eax
15+
movl %eax, %edi
16+
cltq
17+
movl (%r8,%rax,4), %eax
18+
cmpl %esi, %eax
19+
je .L8
20+
movl $0, %ecx
21+
jmp .L3
22+
.L6:
23+
leal (%rcx,%rdx), %eax
24+
movl %eax, %edi
25+
shrl $31, %edi
26+
addl %edi, %eax
27+
sarl %eax
28+
movl %eax, %edi
29+
cltq
30+
movl (%r8,%rax,4), %eax
31+
cmpl %esi, %eax
32+
je .L9
33+
.L3:
34+
cmpl %eax, %esi
35+
jle .L4
36+
leal 1(%rdi), %ecx
37+
jmp .L5
38+
.L4:
39+
leal -1(%rdi), %edx
40+
.L5:
41+
cmpl %edx, %ecx
42+
jle .L6
43+
movl $0, %eax
44+
ret
45+
.L7:
46+
movl $0, %eax
47+
ret
48+
.L8:
49+
movl $1, %eax
50+
ret
51+
.L9:
52+
movl $1, %eax
53+
ret
54+
.LFE0:
55+
.size mystery, .-mystery
56+
.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4"
57+
.section .note.GNU-stack,"",@progbits

0 commit comments

Comments
 (0)