Commit 78d0483a authored by Adam Blank's avatar Adam Blank
Browse files

Initial commit

parents
No related merge requests found
Pipeline #11848 canceled with stages
Showing with 127 additions and 0 deletions
+127 -0
CC = gcc
CFLAGS = -Wall -Werror
all: factmain
# perhaps use this as you develop further parts
# of the assignment
#all: factmain gcdmain
factmain.o: fact.h factmain.c
fact.o: fact.s
gcdmain.o: gcd.h gcdmain.c
gcd.o: gcd.s
factmain: fact.o factmain.o
$(CC) $(CFLAGS) -o factmain fact.o factmain.o
gcdmain: gcd.o gcdmain.o
$(CC) $(CFLAGS) -o gcdmain gcd.o gcdmain.o
clean:
rm -f *.o factmain gcdmain
.PHONY: all clean
Answers to Practice Problem 3.6, CS:APP3e pp.192-193
----------------------------------------------------
Answer to Problem 3.63, CS:APP3e pp.314-315
-------------------------------------------
int ex(int a, int b, int c, int d) {
int res;
res = a * (b - c) + d;
return res;
}
/*
* This is a stub header for fact.s
*/
extern int fact(int n);
#=============================================================================
# The fact(n) function recursively computes n! (n factorial).
#
.globl fact
fact:
orl $0, %edi # Sets zero flag if arg equals zero
jnz fact_continue # Compute factorial if nonzero
movl $1, %eax # Otherwise return 1
jmp fact_return
fact_continue:
pushq %rdi # Save our argument n before recursive call
decl %edi # Compute n-1 for arg in recursive call
call fact # Make recursive call; answer will be in eax
fact_resume:
popq %rdi # Restore our argument n into rdi
# fact(n-1) is in %eax; multiply %edi (n) into that for our answer!
imull %edi, %eax
fact_return:
ret # All done
/* This file contains x86-64 assembly-language implementations of three
* basic, very common math operations.
*/
.text
/*====================================================================
* int f1(int x, int y)
*/
.globl f1
f1:
movl %edi, %edx
movl %esi, %eax
cmpl %edx, %eax
cmovg %edx, %eax
ret
/*====================================================================
* int f2(int x)
*/
.globl f2
f2:
movl %edi, %eax
movl %eax, %edx
sarl $31, %edx
xorl %edx, %eax
subl %edx, %eax
ret
/*====================================================================
* int f3(int x)
*/
.globl f3
f3:
movl %edi, %edx
movl %edx, %eax
sarl $31, %eax
testl %edx, %edx
movl $1, %edx
cmovg %edx, %eax
ret
What does the assembly code do?
-------------------------------
Stack Contents of fact(3)
-------------------------
Address Value Description
-------- -------- ------------------------------------
0x1008
Late tokens applied to this assignment: 0
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment