Beruflich Dokumente
Kultur Dokumente
IA32 Instructions
and
Assembler Directives
31
16 15
CS 217
87
AH
BH
AL
BL
CH
DH
CL
DL
AX
BX
CX
DX
SI
DI
BP
SP
Segment Registers
EIP Register
232-1
15
Unconditional jump
Conditional jump
Procedure call
Return
4Gbytes
address
space
0
3
EFLAG Register
31
Reserved (set to 0)
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
I V V A V R
N IO O D I T S Z 0 A 0 P 1 C
D PI FI C M F 0 T P
F
F
F
L F F F F F F
Identification flag
Virtual interrupt pending
Virtual interrupt flag
Alignment check
Virtual 8086 mode
Resume flag
Nested task flag
I/O privilege level
Overflow flag
Direction flag
Interrupt enable flag
Trap flag
Sign flag
Zero flag
Auxiliary carry flag or adjust flag
Parity flag
Carry flag
Other Registers
Instruction
Opcode
What to do
Source operands
MMX
Destination operand
System
Register
Memory
I/O port
I/O ports
Control registers (CR0, , CR4)
Memory management registers (GDTR, IDTR, LDTR)
Debug registers (DR0, , DR7)
Machine specific registers
Machine check registers
Performance monitor registers
7
Accessing Memory
Syntax:
movl
500, %ecx
$5, %eax
movl
%eax, %ebx
(%eax), %edx
Address Computation
10
Examples
Constant
12
Types of Instructions
push{w,l} source
pushl %ebx
# equivalent instructions
#
subl $4, %esp
#
movl %ebx, (%esp)
pop{w,l} dest
System
TOP
Stack
BOTTOM
14
Arithmetic Instructions
Simple instructions
Simple instructions
Multiply
%ESP
popl %ebx
# equivalent instructions
#
movl (%esp), %ebx
#
addl $4, %esp
SIMD
Logic shift
rotation shift
Bit scan
Bit test
Byte set on conditions
Divide
15
16
Signed Integers
Unsigned Ints
Signed-Magnitude
0000
0001
0010
1001
0011
+1011
+11
0100
-----
-----
10100
20
0101
0110
0111
1000
1001
1010
10
1011
11
1100
12
1101
13
1110
14
1111
15
Signed Integers
4-bit patterns
Unsigned Ints
18
Signed Integers
Signed
Magnitude
0000
+0
0001
+1
0010
+2
0011
+3
0100
+4
0101
+5
0110
+6
0111
+7
1000
-0
1001
-1
1010
10
-2
1011
11
-3
1100
12
-4
1101
13
-5
1110
14
-6
1111
15
-7
Ones Complement
For positive integer k, -k has representation (2n 1) k
Example: 0101 is +5, 1010 is -5
Negating k is same as flipping all the bits in k
Left-most bit is the sign bit
Addition and subtraction are easy
For positive a and b,
a b = a b + 2n 1 + 1
= a + b1C + 1
Two representations of 0: 0000 and 1111
19
20
Signed Integers
4-bit patterns
Unsigned Ints
Signed Integers
Signed
Magnitude
Ones
Complement
0000
+0
+0
0001
+1
+1
0010
+2
+2
0011
+3
+3
0100
+4
+4
0101
+5
+5
0110
+6
+6
0111
+7
+7
1000
-0
-7
1001
-1
-6
1010
10
-2
-5
1011
11
-3
-4
1100
12
-4
-3
1101
13
-5
-2
1110
14
-6
-1
1111
15
-7
-0
Twos Complement
For positive integer k, -k has representation 2n k
Example: 0101 is +5, 1011 is -5
Negating k is same as flipping all the bits in k and then adding 1
Left-most bit is the sign bit
Addition and subtraction are easy
For positive a and b,
a b = a b + 2n
= a + b2C
21
Signed Integers
4-bit patterns
Unsigned Ints
22
Signed Integers
Signed
Magnitude
Ones
Complement
Twos
Complement
4-bit patterns
Unsigned Ints
Signed
Magnitude
Ones
Complement
Twos
Complement
0000
+0
+0
+0
0000
+0
+0
+0
0001
+1
+1
+1
0001
+1
+1
+1
0010
+2
+2
+2
0010
+2
+2
+2
0011
+3
+3
+3
0011
+3
+3
+3
0100
+4
+4
+4
0100
+4
+4
+4
0101
+5
+5
+5
0101
+5
+5
+5
0110
+6
+6
+6
0110
+6
+6
+6
0111
+7
+7
+7
0111
+7
+7
+7
1000
-0
-7
-8
1000
-0
-7
-8
1001
-1
-6
-7
1001
-1
-6
-7
1010
10
-2
-5
-6
1010
10
-2
-5
-6
1011
11
-3
-4
-5
1011
11
-3
-4
-5
1100
12
-4
-3
-4
1100
12
-4
-3
-4
1101
13
-5
-2
-3
1101
13
-5
-2
-3
1110
14
-6
-1
-2
1110
14
-6
-1
-2
1111
15
-7
-0
-1
1111
15
-7
-0
-1
23
24
Branching Instructions
Unconditional branch
jmp addr
Conditional branch
Perform arithmetic operation
Test flags in the EFLAGS register and jump
cmpl %ebx, %eax
je
L1
...
# ebx != eax
...
# ebx == eax
# eax - ebx
L1:
25
26
(ZF = 1)
(ZF = 0)
Equal or zero
Not equal or not zero
C program
(SF ^ OF = 1)
((SF ^ OF) || ZF) = 1)
((SF ^ OF) || ZF) = 0)
(SF ^ OF = 0)
movl a, %eax
cmpl b, %eax
jle L1
if (a > b)
Assembly program
Less than
Less or equal
Greater than
Greater or equal
c = a;
movl a, %eax
movl %eax, c
jmp
L2
# a-b
# jump if a <= b
# a > b branch
(CF = 1)
(CF = 1 || ZF = 1)
(CF = 0 && ZF = 0)
(CF = 0)
L1:
Below
Below or equal
Above
Above or equal
else
c = b;
# a <= b branch
movl b, %eax
movl %eax, c
L2:
# finish
28
Assembler Directives
C program
Allocate/initialize memory
...
}
Assembly program
movl $0, %edx
loop_begin:
cmpl $100, %edx
jge loop_end
...
incl %edx
jmp loop_begin
loop_end:
# i = 0
# i 100
# end if i >= 100
# i++
29
Identifying Sections
30
Initializing Data
.section .data
OS
p:
.byte 215
Text
q:
r:
Data
# three long-words
BSS
Heap
.align 4
.long 555
Stack
0xffffffff
31
32
150,145,154,154,157,0
# a sequence of bytes
.ascii hello
.byte 0
Example
.ascii hello\0
.asciz hello
# ASCII with \0
.string hello
# same as .asciz
.section .bss
.equ
BUFSIZE 512
.lcomm BUF, BUFSIZE
.comm x, 4, 4
#
#
#
#
#
#
or just .bss
define a constant
allocate 512 bytes
local memory for BUF
allocate 4 bytes for x
with 4-byte alignment
34
Summary
Default is local
Instructions manipulate registers and memory
.globl symbol
Example: int x = 1;
.section .data
.globl x
.align 4
x: .long 2
Branch instructions
The six flags in the EFLAGS register
Conditional branching
Example: foo(void){...}
.text
.globl foo
foo:
...
leave
return
35
Define sections
Allocate memory
Initialize values
Make labels externally visible
36