CS 330 Lecture 16 – Functions in Assembly
Agenda
- what ?s
- finish heads/tails
- summing user input
- a multiply function
TODO
- I’ll be gone Wednesday and Friday. Please take advantage of this time to work on CSE and Regexercise. I will have limited time to communicate, though I’ll try to check Piazza once a day.
Code
boolean.s
.section .data
heads_message:
.asciz "%d -> heads\n"
tails_message:
.asciz "%d -> tails\n"
.section .text
.globl main
main:
# Get number of seconds since 1/1/1970.
pushl $0
call time
addl $4, %esp
# Seed that PRNG.
pushl %eax
call srand
addl $4, %esp
call rand
subl $4, %esp
movl %eax, (%esp)
andl $1, %eax
cmp $0, %eax
je heads
tails:
pushl (%esp)
pushl $tails_message
call printf
addl $8, %esp
jmp quit
heads:
pushl (%esp)
pushl $heads_message
call printf
addl $8, %esp
quit:
pushl $0
call exit
repeated_addition.s
.section .data
out_format:
.asciz "The product of %d and %d is %d.\n"
a:
.long 13
b:
.long 12
.section .text
.globl main
main:
pushl b
pushl a
call mult
addl $8, %esp
# %eax holds the product
pushl %eax
pushl b
pushl a
pushl $out_format
call printf
addl $16, %esp
pushl $0
call exit
mult:
pushl %ebp
movl %esp, %ebp
movl $0, %ecx # Our counter
movl $0, %eax # Our accumulating total
loop:
cmpl %ecx, 12(%ebp)
je done
addl 8(%ebp), %eax
incl %ecx
jmp loop
done:
popl %ebp
ret
Haiku
on the halting problem
My function just broke
Told the store, “It won’t return”
“Exactly,” they said
My function just broke
Told the store, “It won’t return”
“Exactly,” they said