CS 330 Lecture 10 – Assembly
Agenda
- what ?s
- assembly ways of doing:
- loops
- structs
- local variables
- functions
TODO
- Read pages 35-45 inĀ Programming From The Ground Up. 1/4 sheet.
Code
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
pushl %eax
# %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:
popl %eax
pushl %eax
pushl $out_format
call printf
addl $8, %esp
pushl %eax
call exit
loop.s
.section .data
out_format:
.asciz "The sum is %d\n"
nums:
.long 12,17,5,4652
count:
.long 4
.section .text
.globl main
main:
movl $0, %ebx # ebx holds the sum
movl $0, %edi # edi holds i
loop:
cmpl count, %edi
jge quit
addl nums(,%edi,4), %ebx
incl %edi
jmp loop
quit:
pushl %ebx
pushl $out_format
call printf
addl $8, %esp
pushl $0
call exit
struct.s
.section .data
in_format:
.asciz "%s %d"
out_format:
.asciz "Person %s is %d years old\n"
.section .text
.globl main
main:
movl %esp, %ebp
subl $52, %esp
leal 4(%ebp), %eax
pushl %eax
call get_person
addl $4, %esp
leal 4(%ebp), %eax
pushl %eax
call put_person
addl $4, %esp
pushl $0
call exit
get_person:
pushl %ebp # preserve main's foothold
movl %esp, %ebp # establish my foothold
movl 8(%ebp), %eax # Grab first parameter, throw it in %eax
leal 48(%eax), %ebx # Load age pointer into %ebx
pushl %ebx # Load up age pointer for scanf
pushl %eax # Load up name pointer for scanf
pushl $in_format # Load up format string for scanf
call scanf # call scanf
addl $12, %esp # clean up parameters
movl %ebp, %esp
popl %ebp
ret
put_person:
pushl %ebp # preserve main's foothold
movl %esp, %ebp # establish my foothold
movl 8(%ebp), %eax # Grab first parameter, throw it in %eax
pushl 48(%eax) # Load up age for printf
pushl %eax # Load up name pointer for printf
pushl $out_format # Load up format string for printf
call printf # call printf
addl $12, %esp # clean up parameters
movl %ebp, %esp
popl %ebp
ret
stringloop.s
.section .data
out_format:
.asciz "[%c]\n"
name:
.asciz "abcd\n"
count:
.long 4
.section .text
.globl main
main:
movl $0, %ebx # ebx holds the sum
movl $0, %edi # edi holds i
loop:
cmpl count, %edi
jge quit
movl name(,%edi,1), %eax
pushl %eax
pushl $out_format
call printf
addl $8, %esp
incl %edi
jmp loop
quit:
pushl %ebx
pushl $out_format
call printf
addl $8, %esp
pushl $0
call exit
Haiku
We ride the past’s waves
Could it have been otherwise?
With some other seed?
Could it have been otherwise?
With some other seed?