Assembly Language: Overview!: Jennifer Rexford!
Assembly Language: Overview!: Jennifer Rexford!
Assembly Language: Overview!: Jennifer Rexford!
Jennifer Rexford!
1
Goals of this Lecture!
2
Context of this Lecture!
Second half of the course!
3
Three Levels of Languages!
4
High-Level Language!
Make programming
easier by describing
operations in a semi- count = 0;
natural language! while (n > 1) {
Increase the portability count++;
if (n & 1)
of the code!
n = n*3 + 1;
One line may involve else
many low-level n = n/2;
operations! }
Examples: C, C++,
Java, Pascal, ! 5
Assembly Language!
Tied to the specifics movl $0, %ecx
loop:
of the underlying cmpl $1, %edx
machine! jle endloop
addl $1, %ecx
Commands and movl %edx, %eax
names to make the andl $1, %eax
je else
code readable and movl %edx, %eax
writeable by humans! addl %eax, %edx
addl %eax, %edx
Hand-coded addl $1, %edx
jmp endif
assembly code may else:
sarl $1, %edx
be more efficient! endif:
jmp loop
E.g., IA-32 from Intel! endloop: 6
Machine Language!
Also tied to the
underlying machine!
0000 0000 0000 0000 0000 0000 0000 0000
What the computer sees 0000 0000 0000 0000 0000 0000 0000 0000
9222 9120 1121 A120 1121 A121 7211 0000
and deals with! 0000 0001 0002 0003 0004 0005 0006 0007
0008 0009 000A 000B 000C 000D 000E 000F
Every instruction is a 0000 0000 0000 FE10 FACE CAFE ACED CEDE
sequence of one or
more numbers! 1234 5678 9ABC DEF0 0000 0000 F00D 0000
0000 0000 EEEE 1111 EEEE 1111 0000 0000
All stored in memory on B1B2 F1F5 0000 0000 0000 0000 0000 0000
8
Why Learn Intel IA-32 Assembly?!
Program natively on our computing platform!
Rather than using an emulator to mimic another machine!
10
A Typical Computer !
Memory Chipset
I/O bus
ROM
Network
11
Von Neumann Architecture!
Central Processing Unit!
CPU
Control unit! Control
Fetch, decode, and execute ! Unit
Arithmetic and logic unit! ALU
Execution of low-level operations!
General-purpose registers!
Registers
High-speed temporary storage!
Data bus!
Provide access to memory!
Data bus
Random Access
Memory (RAM)
12
Von Neumann Architecture!
Memory!
CPU
Store executable machine-language Control
instructions (text section)! Unit
Store data (rodata, data, bss, heap, ALU
and stack sections)!
TEXT! Registers
RODATA!
DATA!
BSS!
Data bus
HEAP!
Random Access
Memory (RAM)
STACK! 13
Control Unit: Instruction Pointer!
Stores the location of the next instruction!
Address to use when reading machine-language
instructions from memory (i.e., in the text section)!
EIP
14
Control Unit: Instruction Decoder!
Determines what operations need to take place!
Translate the machine-language instruction !
operation
ALU ALU flag/carry
15
dst
Registers!
Small amount of storage on the CPU!
Can be accessed more quickly than main memory!
EAX
EBX
18
Kinds of Instructions!
Reading and writing data!
count = 0!
n!
count = 0;
Arithmetic and logic operations!
while (n > 1) { Increment: count++!
count++; Multiply: n * 3!
Divide: n/2!
if (n & 1)
Logical AND: n & 1!
n = n*3 + 1;
Checking results of comparisons !
else Is (n > 1) true or false?!
n = n/2; Is (n & 1) non-zero or zero?!
count = 0;
while (n > 1) {
count++;
Registers
if (n & 1) n %edx
n = n*3 + 1; count %ecx
else
n = n/2;
}
count=0;
while (n>1) {
count++;
movl %edx, %eax
if (n&1) andl $1, %eax
n = n*3+1;
else
n = n/2;
}
count=0;
while (n>1) {
count++;
if (n&1)
movl %edx, %eax
n = n*3+1; addl %eax, %edx
else addl %eax, %edx
addl $1, %edx
n = n/2;
}
count=0;
while (n>1) {
count++;
if (n&1)
n = n*3+1;
else
n = n/2; sarl $1, %edx
}
26
Jump and Labels: While Loop!
loop:
cmpl $1, %edx
jle endloop
while (n>1) {
Checking if EDX
is less than or
equal to 1.
jmp loop
endloop:
27
Jump and Labels: While Loop!
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
addl $1, %ecx
while (n>1) {
movl %edx, %eax
count++; andl $1, %eax
if (n&1) je else
movl %edx, %eax
n = n*3+1; addl %eax, %edx
else addl %eax, %edx
addl $1, %edx
n = n/2; jmp endif
else:
} sarl $1, %edx
endif:
jmp loop
endloop:
28
Jump and Labels: If-Then-Else!
29
Jump and Labels: If-Then-Else!
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
addl $1, %ecx
while(n>1) {
movl %edx, %eax
count++; andl $1, %eax
if (n&1) je else
movl %edx, %eax
n = n*3+1; addl %eax, %edx
else then block addl %eax, %edx
addl $1, %edx
n = n/2; jmp endif
else:
} else block sarl $1, %edx
endif:
jmp loop
endloop:
30
Making the Code More Efficient!
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
addl $1, %ecx
while(n>1) {
movl %edx, %eax
count++; andl $1, %eax
if (n&1) je else
movl %edx, %eax
n = n*3+1; addl %eax, %edx
else addl %eax, %edx
addl $1, %edx
n = n/2; jmp endif
else:
} sarl $1, %edx
endif:
Replace with jmp loop
jmp loop endloop:
31
n %edx
Complete Example! count %ecx
movl $0, %ecx
loop:
cmpl $1, %edx
count=0; jle endloop
addl $1, %ecx
while (n>1) {
movl %edx, %eax
count++; andl $1, %eax
if (n&1) je else
movl %edx, %eax
n = n*3+1; addl %eax, %edx
else addl %eax, %edx
addl $1, %edx
n = n/2; jmp endif
else:
} sarl $1, %edx
endif:
jmp loop
endloop:
32
Reading IA-32 Assembly Language!
Referring to a register: percent sign (%)!
E.g., %ecx or %eip!
Referring to immediate operand: dollar sign ($)!
E.g., $1 for the number 1!
Storing result: typically in the second argument!
E.g. addl $1, %ecx increments register ECX !
E.g., movl %edx, %eax moves EDX to EAX!
Assembler directives: starting with a period (.)!
E.g., .section .text to start the text section of memory!
Comment: pound sign (#)!
E.g., # Purpose: Convert lower to upper case!
33
Conclusions!
Assembly language!
In between high-level language and machine code!
Programming the bare metal of the hardware!
Loading and storing data, arithmetic and logic
operations, checking results, and changing control flow!
34