C Program Translation - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

C Program Translation

Description:

Program Translation. C is closer to assembly than almost any other language. Arithmetic/logic/bitwise operations resemble machine operations ... – PowerPoint PPT presentation

Number of Views:47
Avg rating:3.0/5.0
Slides: 26
Provided by: dianet4
Category:

less

Transcript and Presenter's Notes

Title: C Program Translation


1
C Program Translation
  • Translating C programs into assembly

2
Program Translation
  • C is closer to assembly than almost any other
    language
  • Arithmetic/logic/bitwise operations resemble
    machine operations
  • The use of pointers corresponds to memory access
    at machine level
  • No high-level language complex elements

3
Program Translation
  • For now, use pseudo assembly instructions
  • Will see full details of assembly
  • Use of registers
  • Denote registers as r0, r1, r2,
  • Registers can hold byte, half-word (short), word
    (integer)
  • r0 holds zero

4
Program Translation
  • Memory operations
  • Load Load a memory variable at a given memory
    address to a register
  • Store Store a register value into a memory
    variable at a give memory address
  • No other operations are allowed on memory
    variables
  • load r1, a r1 lt mema
  • store r2, b memb lt r1
  • store r3, 0x40000012 mem0x40000012 lt r3

5
Program Translation
  • Data operations
  • Add , sub -, mul , div /, and , or , xor ,
    shift-left ltlt, shift-right gtgt
  • Can use two registers
  • Can use one register, one constant
  • Format
  • add r1, r2, r3
  • add r1, r1, 100

6
Program Translation
  • Compare instruction cmp
  • Can compare two registers
  • Can compare one register, one constant
  • Cannot use memory
  • cmp r1, r2
  • cmp r1, 0

7
Program Translation
  • Branch instructions non-sequential control flow.
  • (1) Branch condition-when (2) Branch target-where
  • Should follow a compare instruction
  • beq branch if equal
  • bne branch if not equal
  • blt branch if less than
  • ble branch if less than or equal
  • cmp r1, 0
  • beq label

8
Arithmetic Expressions
  • Exercise
  • a b
  • a 10

9
Arithmetic Expressions
  • Exercises
  • c a b
  • e (a b) (c d)

10
Translating if statement
  • C if statement
  • if (cond_expr)
  • then_statement
  • else
  • else_statement

11
Translating if statement
  • goto version
  • t cond_expr
  • if (!t) goto false
  • then_statement
  • goto done
  • flase
  • else_statement
  • done

12
Translating if statement
  • Exercise
  • if (x gt y)
  • max x
  • else
  • max y
  • Write goto version
  • Translate into pseudo-assembly

13
Translating if statement
  • Exercise
  • if (x gt 0)
  • x 0
  • Write goto version
  • Translate into pseudo-assembly

14
Translating do-while Loop
  • C do-while statement
  • do
  • body_statement
  • while (cond_expr)

15
Translating do-while Loop
  • Goto version
  • loop
  • body_statement
  • t cond_expr
  • if (t) goto loop

16
Translating while Loop
  • Exercise Calculate the parity bit of n
  • m n
  • parity 0
  • do
  • parity (m 1)
  • m gtgt 1
  • while (m ! 0)

17
Translating while Loop
  • C while statement
  • while (cond_expr)
  • body_statement

18
Translating while Loop
  • goto version 1
  • loop
  • t cond_expr
  • if (!t) goto done
  • body_statement
  • goto loop
  • done

19
Translating while Loop
  • Goto version 2
  • goto test
  • loop
  • body_statement
  • test
  • t cond_expr
  • if (t) goto loop
  • done

20
Translating while Loop
  • Exercise Calculate the parity bit of n
  • m n
  • parity 0
  • while (m ! 0)
  • parity (m 1)
  • m gtgt 1

21
Translating for Loop
  • C for statement
  • for (init_expr cond_expr update_expr)
  • body_statement

22
Translating for Loop
  • Translated to while statement
  • init_expr
  • while (cond_expr)
  • body_statement
  • update_expr

23
Translating for Loop
  • Goto version

24
Translating for Loop
  • Exercise Calculate the sum of XN
  • sum 0
  • for (i 0 i lt N i )
  • sum Xi
  • Write goto version
  • Translate into pseudo-assembly

25
Memory Pointers
  • How to use pointers?
  • int pInt m
  • pInt m
Write a Comment
User Comments (0)
About PowerShow.com