EE209A - 24 15 Assembly2
EE209A - 24 15 Assembly2
Electrical Engineering
EE209: Programming Structures for Electrical Engineering
• Programmer-created types
• struct: arbitrary size, depending on the fields
• arrays
• Multiple consecutive elements of some fixed size
• Where each element could be a struct
3
Supporting Different Sizes in IA-32
4
Byte Order in Multi-Byte Entities
• Intel is a little endian architecture
• Least significant byte of multi-byte entity is stored at
lowest memory address
• “Little end goes first” 1000 00000101
1001 00000000
The int 5 at address 1000: 1002 00000000
1003 00000000
• Some other systems use big endian
• Most significant byte of multi-byte entity is stored at
lowest memory address
• “Big end goes first”
1000 00000000
1001 00000000
The int 5 at address 1000: 1002 00000000
1003 00000101
5
Little Endian Example
int main(void) {
int i=0x003377ff, j;
unsigned char *p = (unsigned char *) &i;
Byte 0: ff
Output on a little-endian
Byte 1: 77
machine Byte 2: 33
Byte 3: 0
7
IA-32 General Purpose Registers
AX/BX/CX/DX
31 15 8 7 0 16-bit 32-bit
AH AL AX EAX
BH BL BX EBX
CH CL CX ECX
DH DL DX EDX
SI ESI
DI EDI
General-purpose registers
8
9
C Example: One-Byte Data
Global char variable i is in %al, the lower byte of the “A” register.
10
C Example: Four-Byte Data
Global int variable i is in %eax, the full 32 bits of the “A” register.
11
Loading and Storing Data
• Immediate addressing
• Example: movl $0, %ecx
• Data (e.g., number “0”) embedded in the instruction
• Initialize register ECX with zero
• Register addressing
• Example: movl %edx, %ecx
• Choice of register(s) embedded in the instruction
• Copy value in register EDX into register ECX
12
Accessing Memory
13
(1) Direct Addressing
• IA-32 example:
14
(2) Indirect Addressing
• IA-32 example:
15
(3) Base Pointer Addressing
• Load or store with an offset from a base address
• Register storing the base address
• Fixed offset also embedded in the instruction
• Instruction computes the address and does access
• IA-32 example:
16
(4) Indexed Addressing
• Load or store with an offset and multiplier
• Fixed based address embedded in the instruction
• Offset computed by multiplying register with constant
• Instruction computes the address and does access
• IA-32 example:
17
Indexed Addressing Example
18
Effective Address: More Generally
eax eax
ebx ebx None
ecx ecx 1
edx edx 2 8-bit
Offset = esp + esp * +
ebp 4 16-bit
ebp
esi esi 8 32-bit
edi edi
19
Effective Address (source)
20
Review with Examples (source – page 2)
Assembly C pseudocode
movl %eax,%edx edx = eax;
21
Data Access Methods: Summary
• Indexed addressing: instruction contains base address, and specifies an index register and a multiplier (1, 2, 4, or 8)
• movl 2000(,%eax,1), %ecx
22
Goals of this Lecture
23
Control Flow
• Common case
• Execute code sequentially
• One instruction after another
24
Condition Codes
25
Condition Codes (continued)
26
Carry Flag
• The rules for turning on the carry flag in binary/integer math are two:
1. The carry flag is set if the addition of two numbers causes a carry out of the most significant (leftmost) bits
added.
- 1111 + 0001 = 0000 (carry flag is turned on)
2. The carry (borrow) flag is also set if the subtraction of two numbers requires a borrow into the most significant
(leftmost) bits subtracted.
- 0000 - 0001 = 1111 (carry flag is turned on)
In unsigned arithmetic, watch the carry flag to detect errors. In signed arithmetic, the carry flag tells you nothing
interesting.
27
Overflow Flag
• The rules for turning on the overflow flag in binary/integer math are two:
1. If the sum of two numbers with the sign bits off yields a result number with the sign bit on, the "overflow" flag is
turned on.
• 0100 + 0100 = 1000 (overflow flag is turned on)
2. If the sum of two numbers with the sign bits on yields a result number with the sign bit off, the "overflow" flag is
turned on.
• 1000 + 1000 = 0000 (overflow flag is turned on)
Otherwise, the overflow flag is turned off. * 0100 + 0001 = 0101 (overflow flag is turned off)
• 0110 + 1001 = 1111 (overflow flag is turned off)
• 1000 + 0001 = 1001 (overflow flag is turned off)
• 1100 + 1100 = 1000 (overflow flag is turned off)
28
Overflow Flag
• Note that you only need to look at the sign bits (leftmost) of the three numbers to decide if the overflow flag is
turned on or off.
• If you are doing two's complement (signed) arithmetic, overflow flag on means the answer is wrong - you added
two positive numbers and got a negative, or you added two negative numbers and got a positive.
• If you are doing unsigned arithmetic, the overflow flag means nothing and should be ignored.
29
Example Five-Bit Comparisons
• Comparison: cmp $6, $12
• Not zero: ZF=0 (diff is not 00000) 01100 01100
• Positive: SF=0 (first bit is 0) - 00110 + 11010
?? 00110
• No carry: CF=0 (unsigned diff is correct)
• No overflow: OF=0 (signed diff is correct)
• Comparison: cmp $12, $6
• Not zero: ZF=0 (diff is not 00000) 00110 00110
- 01100 + 10100
• Negative: SF=1 (first bit is 1) 11010
??
• Carry: CF=1 (unsigned diff is wrong)
• No overflow: OF=0 (signed diff is correct)
10100 10100
• Comparison: cmp $-6, $-12 - 11010 + 00110
?? 11010
• Not zero: ZF=0 (diff is not 00000)
• Negative: SF=1 (first bit is 1)
• Carry: CF=1 (unsigned diff of 20 and 26 is wrong)
• No overflow: OF=0 (signed diff is correct)
30
Jumps after Comparison (cmpl)
• Equality
• Equal: je (ZF)
• Not equal: jne (~ZF)
31
Branch Instructions
• Conditional jump
• j{l,g,e,ne,...} target if (condition) {eip = target}
Comparison Signed Unsigned
= e e “equal”
≠ ne ne “not equal”
> g a “greater,above”
≥ ge ae “...-or-equal”
< l b “less,below”
≤ le be “...-or-equal”
overflow/carry o c
no ovf/carry no nc
• Unconditional jump
• jmp target
• jmp *register (e.g., jmp *%eax) # jump to the address that
%eax holds
• jmp *(register) (e.g., jmp *(%eax)) # %eax is a pointer to
the target.
32
Jumping
33
Jumping (continued)
do { loop:
Body; Body;
} while (Test); if (Test) then goto loop;
34
Jumping (continued)
Init;
if (!Test) goto done;
loop:
Body;
Update;
if (Test) goto loop;
done:
35
Arithmetic Instructions
• Simple instructions
• add{b,w,l,q} source, dest dest = source + dest
• sub{b,w,l,q} source, dest dest = dest – source
• inc{b,w,l,q} dest dest = dest + 1
• dec{b,w,l,q} dest dest = dest – 1
• neg{b,w,l,q} dest dest = ~dest + 1 # 2’s complement
• cmp{b,w,l,q} source1, source2 source2 – source1
• Multiply
• mul (unsigned) or imul (signed)
mull %ebx # edx, eax = eax * ebx
• Divide
• div (unsigned) or idiv (signed)
idivl %ebx # edx:eax / ebx
# quotient: eax remainder: edx
• Many more in Intel manual (volume 2)
• adc (add with carry), sbb (integer subtraction with borrow), decimal
arithmetic instructions
36
Arithmetic Instructions
mulb src
src X AL AX
mulw src
src X AX DX AX
mull src
37
Bitwise Logic Instructions
• Simple instructions
and{b,w,l,q} source, dest dest = source & dest
• Rotation shift
• Bit scan
• Bit test
• Byte set on conditions
38
Data Transfer Instructions
• pop{w,l,q} dest
popl %ebx # equivalent instructions
movl (%esp), %ebx
addl $4, %esp
39
Data Transfer Instructions
• mov{b,w,l,q} source, dest
• pop{w,l,q} dest
popl %ebx # equivalent instructions
movl (%esp), %ebx
addl $4, %esp
• pop{w,l,q} dest
popl %ebx # equivalent instructions
movl (%esp), %ebx
addl $4, %esp
• pop{w,l,q} dest
popl %ebx # equivalent instructions
movl (%esp), %ebx
addl $4, %esp
42
Data Transfer Instructions
• mov{b,w,l,q} source, dest
• pop{w,l,q} dest
popl %ebx # equivalent instructions
movl (%esp), %ebx
esp
addl $4, %esp
43
Data Transfer Instructions
• mov{b,w,l,q} source, dest
• pop{w,l,q} dest
popl %ebx # equivalent instructions
movl (%esp), %ebx
addl $4, %esp esp
• Many more in Intel manual (volume 2)
44
Conclusions
• Accessing data
• Byte, word, and long-word data types
• Wide variety of addressing modes
• Control flow
• Common C control-flow constructs
• Condition codes and jump instructions
• Manipulating data
• Arithmetic and logic operations
• Next time
• Calling functions, using the stack
45
46