MachineLevel Representation of Programs II - PowerPoint PPT Presentation

1 / 47
About This Presentation
Title:

MachineLevel Representation of Programs II

Description:

A linear array of bytes. each with its own unique address (array ... Disassembly output. 0x80483b4 sum : 0x80483b4 55. 0x80483b5 89 e5. 0x80483b7 8b 45 0c ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 48
Provided by: binyu5
Category:

less

Transcript and Presenter's Notes

Title: MachineLevel Representation of Programs II


1
Machine-Level Representation of Programs II
2
Outline
  • Assembly code and object code
  • Memory and Registers
  • Suggested reading
  • Chap 3.2, 3.3, 3.4

3
Hardware Organization
4
Virtual spaces
  • A linear array of bytes
  • each with its own unique address (array index)
    starting at zero

0xffffffff 0xfffffffe 0x2 0x1 0x0
contents
addresses
5
C constructs
  • Variable
  • Different data types can be declared
  • Operation
  • Arithmetic expression evaluation
  • control
  • Loops
  • Procedure calls and returns

6
Code Examples
7
Code Examples
8
C Code
  • Add two signed integers
  • int t xy

9
Assembly 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

10
Assembly Programmers View
11
Programmer-Visible States
  • Program Counter(eip)
  • Address of the next instruction
  • Register File
  • Heavily used program data
  • Integer and floating-point

12
Programmer-Visible States
  • Conditional code register
  • Hold status information about the most recently
    executed instruction
  • Implement conditional changes in the control flow

13
Operands
  • In high level languages
  • Either constants
  • Or variable
  • Example
  • A A 4

14
Where are the variables? registers Memory
15
Operands
  • Counterparts in assembly languages
  • Immediate ( constant )
  • Register ( variable )
  • Memory ( variable )
  • Example
  • movl 8(ebp), eax
  • addl 4, eax

16
Simple 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

17
Virtual spaces
  • A linear array of bytes
  • each with its own unique address (array index)
    starting at zero

0xffffffff 0xfffffffe 0x2 0x1 0x0
contents
addresses
18
Memory 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

19
Indexed 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

20
Memory Addressing Mode
  • The address represented by the above form
  • imm REb REi s
  • It gives the value
  • Mimm REb REi s

21
Addressing Mode
22
Value
Operand
0x100
eax
0xFF
(eax)
0x108
0x108
0x13
0x108
(0x108)0x13
260(ecx,edx)
(0x10C)0x11
(eax,edx,4)
23
Code Examples
24
Code Examples
25
Code Examples
26
Object Code
  • 3-byte instruction
  • Stored at address 0x80483ba
  • 0x80483ba 03 45 08

27
Understanding 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

28
Operations 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

29
Data 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)
31
Data Formats
32
Data Formats
  • Move data instruction
  • mov (general)
  • movb (move byte)
  • movw (move word)
  • movl (move double word)

33
Move Instructions
  • Format
  • mov src, dest
  • src and dest can only be one of the following
  • Immediate
  • Register
  • Memory

34
Move Instructions
  • Format
  • The only possible combinations of the (src, dest)
    are
  • (immediate, register)
  • (memory, register) load
  • (register, register)
  • (immediate, memory) store
  • (register, memory) store

35
Data Movement
36
Data 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

37
Data Movement Example
  • Initial value dh8d eax 98765432
  • movb dh, al eax9876548d
  • movsbl dh, eax eaxffffff8d
  • 3 movzbl dh, eax eax0000008d

38
Stack operations
Increasing address
0x108
esp
Stack top
39
Stack operations
pushl eax
0x108
0x104
esp
Stack top
40
Stack operations
popl edx
esp
0x108
0x104
Stack top
41
Data 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)

42
Data 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

43
Data Movement Example
44
Data Movement Example
1 pushl ebp
45
Data Movement Example
2 movl esp, ebp
46
Data Movement Example
3 movl 8(ebp), eax 4 movl 12(ebp),
edx 5 movl (eax), ecx 6 movl
edx, (eax) 7 movl ecx, eax
47
Data Movement Example
8 movl ebp, esp 9 popl ebp
Write a Comment
User Comments (0)
About PowerShow.com