CS 330 Lecture 9 – Assembly
Agenda
- what ?s
- think about this
- the assembly way of doing things
- getting input from user
- heads/tails generator
- summing up a list of numbers
- using local variables
- binding data together
TODO
- Read pages 19-31 and 109-114 of Programming From The Ground Up. 1/4 sheet.
Think About This
- Why does altering a parameter in a function have no lasting effect?
- What purpose do
.long
,.ascii
,.byte
,.short
, etc., serve? - What is the defining characteristic of an array?
Code
get_two.s
.section .data
# a:
# .long 7
# b:
# .long 8
in_format:
.asciz "%d %d"
.section .bss
.lcomm a, 4
.lcomm b, 4
.section .text
.globl main
main:
pushl $a
pushl $b
pushl $in_format
call scanf
addl $12, %esp
movl a, %eax
addl b, %eax
pushl %eax
call exit
headxortails.s
.section .data
out_format:
.asciz "Your RANDOM number is %d\n"
heads_message:
.asciz "HEAD!"
tails_message:
.asciz "TAIL!"
.section .text
.globl main
main:
pushl $0
call time
addl $4, %esp
# current nseconds since epoch is in %eax
pushl %eax
call srand
addl $4, %esp
call rand
# %eax holds our random number
andl $1, %eax
# andl %eax, $1
# %eax is either 0 or 1
cmpl $0, %eax
je tails
heads:
pushl $heads_message
call printf
addl $4, %esp
jmp quit
tails:
pushl $tails_message
call printf
addl $4, %esp
quit:
pushl %eax
pushl $out_format
call printf
addl $8, %esp
pushl %eax
call exit
Haiku
They didn’t see them
Failures, one through ninety-nine
They called it talent
Failures, one through ninety-nine
They called it talent