Title: MachineLevel Representation of Programs II
1Machine-Level Representation of Programs II
2Outline
- Assembly code and object code
- Memory and Registers
- Suggested reading
- Chap 3.2, 3.3, 3.4
3Hardware Organization
4Virtual spaces
- A linear array of bytes
- each with its own unique address (array index)
starting at zero
0xffffffff 0xfffffffe 0x2 0x1 0x0
contents
addresses
5C constructs
- Variable
- Different data types can be declared
- Operation
- Arithmetic expression evaluation
- control
- Loops
- Procedure calls and returns
6Code Examples
7Code Examples
8C Code
- Add two signed integers
- int t xy
9Assembly Code
- Operands
- x Register eax
- y Memory Mebp8
- t Register eax
- Instruction
- addl 8(ebp),eax
- Add 2 4-byte integers
- Similar to expression x y
- Return function value in eax
10Assembly Programmers View
11Programmer-Visible States
- Program Counter(eip)
- Address of the next instruction
- Register File
- Heavily used program data
- Integer and floating-point
12Programmer-Visible States
- Conditional code register
- Hold status information about the most recently
executed instruction - Implement conditional changes in the control flow
13Operands
- In high level languages
- Either constants
- Or variable
- Example
- A A 4
14Where are the variables? registers Memory
15Operands
- Counterparts in assembly languages
- Immediate ( constant )
- Register ( variable )
- Memory ( variable )
- Example
- movl 8(ebp), eax
- addl 4, eax
16Simple Addressing Mode
- Immediate
- represents a constant
- The format is imm (4, 0xffffffff)
- Registers
- The fastest storage units in computer systems
- Typically 32-bit long
- Register mode Ea
- The value stored in the register
- Noted as REa
17Virtual spaces
- A linear array of bytes
- each with its own unique address (array index)
starting at zero
0xffffffff 0xfffffffe 0x2 0x1 0x0
contents
addresses
18Memory References
- The name of the array is annotated as M
- If addr is a memory address
- Maddr is the content of the memory starting at
addr - addr is used as an array index
- How many bytes are there in Maddr?
- It depends on the context
19Indexed Addressing Mode
- An expression for
- a memory address (or an array index)
- Most general form
- Imm(Eb, Ei, s)
- Constant displacement Imm 1, 2 or 4 bytes
- Base register Eb Any of 8 integer registers
- Index register Ei Any, except for esp
- S Scale 1, 2, 4, or 8
20Memory Addressing Mode
- The address represented by the above form
- imm REb REi s
- It gives the value
- Mimm REb REi s
21Addressing Mode
22Value
Operand
0x100
eax
0xFF
(eax)
0x108
0x108
0x13
0x108
(0x108)0x13
260(ecx,edx)
(0x10C)0x11
(eax,edx,4)
23Code Examples
24Code Examples
25Code Examples
26Object Code
- 3-byte instruction
- Stored at address 0x80483ba
- 0x80483ba 03 45 08
27Understanding Machine Execution
- Where the sequence of instructions are stored?
- In virtual memory
- Code area
- How the instructions are executed?
- eip stores an address of memory
- from the address, machine can read a whole
instruction once - then execute it
- eip is also called program counter (PC)
- increase eip
28Operations in Assembly Instructions
- Performs only a very elementary operation
- Normally one by one in sequential
- Operate data stored in registers
- Transfer data between memory and a register
- Conditionally branch to a new instruction address
29Data layout
- Object model in assembly
- A large, byte-addressable array
- No distinctions even between signed or unsigned
integers - Code, user data, OS data
- Run-time stack for managing procedure call and
return - Blocks of memory allocated by user
30(No Transcript)
31Data Formats
32Data Formats
- Move data instruction
- mov (general)
- movb (move byte)
- movw (move word)
- movl (move double word)
33Move Instructions
- Format
- mov src, dest
- src and dest can only be one of the following
- Immediate
- Register
- Memory
34Move Instructions
- Format
- The only possible combinations of the (src, dest)
are - (immediate, register)
- (memory, register) load
- (register, register)
- (immediate, memory) store
- (register, memory) store
35Data Movement
36Data Movement Example
- movl 0x4050, eax immediate register
- movl ebp, esp register register
- movl (edx, ecx), eax memory register
- movl -17, (esp) immediate memory
- movl eax, -12(ebp) register memory
37Data Movement Example
- Initial value dh8d eax 98765432
- movb dh, al eax9876548d
- movsbl dh, eax eaxffffff8d
- 3 movzbl dh, eax eax0000008d
38Stack operations
Increasing address
0x108
esp
Stack top
39Stack operations
pushl eax
0x108
0x104
esp
Stack top
40Stack operations
popl edx
esp
0x108
0x104
Stack top
41Data Movement Example
- int exchange(int xp, int y)
-
- int x xp / operator performs deferencing
/ - xp y
- return x
-
- int a 4
- int b exchange(a, 3) / address of operator
creates a pointer / - printf(a d, b d\n, a, b)
42Data Movement Example
1 pushl ebp 2 movl esp, ebp 3 movl
8(ebp), eax 4 movl 12(ebp), edx 5 movl
(eax), ecx 6 movl edx, (eax) 7 movl
ecx, eax 8 movl ebp, esp 9 popl ebp
- int exchange(int xp, int y)
-
- int x xp
- xp y
- return x
43Data Movement Example
44Data Movement Example
1 pushl ebp
45Data Movement Example
2 movl esp, ebp
46Data Movement Example
3 movl 8(ebp), eax 4 movl 12(ebp),
edx 5 movl (eax), ecx 6 movl
edx, (eax) 7 movl ecx, eax
47Data Movement Example
8 movl ebp, esp 9 popl ebp