CS 330 Lecture 14 – Assembly
Agenda
- what ?s
- the von Neumann computer
- leaky abstractions
- line at a time
TODO
- Read chapters 1-3 of Programming from the Ground Up. 1/4 sheet.
Recall This
- What’s the von Neumann architecture of a computer?
- What happens in a computer when you add two variables?
- What happens when you execute an executable?
Line at a Time
1: .section .text
2: .globl main
3:
4: main:
5: movl $5, %eax
6: movl $6, %ebx
7: addl %ebx, %eax
8:
9: pushl %eax
10: call exit
1: .section .data
2: out_message:
3: .asciz "What's the difference between %d and %d? %d.\n"
4: prompt_message:
5: .asciz "Enter two numbers: "
6: in_format:
7: .asciz "%d %d"
8: a:
9: .long 20
10: b:
11: .long 4
12:
13: .section .text
14: .globl main
15:
16: main:
17: pushl $prompt_message
18: call printf
19: addl $4, %esp
20:
21: pushl $b
22: pushl $a
23: pushl $in_format
24: call scanf
25: addl $12, %esp
26:
27: movl a, %eax
28: subl b, %eax
29:
30: pushl %eax
31: pushl b
32: pushl a
33: pushl $out_message
34: call printf
35: addl $16, %esp
36:
37: pushl $0
38: call exit
1: .section .data
2: message:
3: .ascii "One past %d is %d.\0"
4:
5: .section .bss
6: .lcomm a, 4
7: .lcomm b, 4
8:
9: .section .text
10: .globl main
11:
12: main:
13: movl $12, %eax
14: movl %eax, a
15: movl %eax, b
16: incl b
17:
18: pushl b
19: pushl a
20: pushl $message
21: call printf
22: addl $12, %esp
23:
24: pushl $0
25: call exit
1: .section .data
2: heads_message:
3: .asciz "%d -> heads\n"
4: tails_message:
5: .asciz "%d -> tails\n"
6:
7: .section .data
8: .globl main
9:
10: main:
11: subl $4, %esp
12:
13: call rand
14: movl %eax, 4(%esp)
15:
16: andl $1, %eax
17: cmpl $0, %eax
18:
19: je tails
20:
21: heads:
22: pushl 4(%esp)
23: pushl $heads_message
24: call printf
25: addl $8, %esp
26: jmp quit
27:
28: tails:
29: pushl 4(%esp)
30: pushl $tails_message
31: call printf
32: addl $8, %esp
33:
34: quit:
35: pushl $0
36: call exit
Haiku
on budget cuts
When atoms leave schools
Our kids are better prepared
To be alchemists
When atoms leave schools
Our kids are better prepared
To be alchemists