Machine Language CS 3330 Samira Khan University of Virginia Feb - - PowerPoint PPT Presentation

machine language
SMART_READER_LITE
LIVE PREVIEW

Machine Language CS 3330 Samira Khan University of Virginia Feb - - PowerPoint PPT Presentation

Machine Language CS 3330 Samira Khan University of Virginia Feb 2, 2017 AG AGENDA Logistics Review of Abstractions Machine Language 2 Lo Logistics Feedback Not clear Hard to hear Use microphone Good feedback


slide-1
SLIDE 1

Samira Khan

University of Virginia Feb 2, 2017

Machine Language

CS 3330

slide-2
SLIDE 2

AG AGENDA

  • Logistics
  • Review of Abstractions
  • Machine Language

2

slide-3
SLIDE 3

Lo Logistics

  • Feedback
  • Not clear
  • Hard to hear
  • Use microphone
  • Good feedback
  • I'm just confused how you think that you've prepared us for the

homework or lab. In class today we learned about logical ANDs. Why the fuck are we learning that? Teach us assembly or something else that is actually useful.

  • I agree these are easy; May be can get rid of binary operations next time
  • Reviewing is still useful
  • More useful things to come throughout the semester
  • The goal is to learn

3

slide-4
SLIDE 4

Lo Logistics

  • Declaring BACS major
  • Applications are open to apply to declare the BACS
  • Deadline is on Feb 20, 2017
  • Full details on how to apply are found here:
  • http://bit.ly/apply-bacs-s17

4

slide-5
SLIDE 5

Lo Logistics

  • Next Lab: In-lab quiz
  • on lab machines
  • during assigned lab times
  • without assistance (TAs will be proctoring)
  • Coding assignment: strlen, strsep
  • https://archimedes.cs.virginia.edu/cs3330/c/lab1.php
  • Interface is available outside of lab
  • But only work done in lab will count
  • We check IPs + submission times
  • ”It's okay if you memorize the code, but we don't think that's best strategy"
  • No homework due before it -- the homework is prepping for it
  • Follow-up "lists in C" homework assignment

5

slide-6
SLIDE 6

Reading for Next Week

  • ISA’s and Y86-64
  • §4.1
  • Sequential Processor
  • §4.2; §4.3.1

6

slide-7
SLIDE 7

AG AGENDA

  • Logistics
  • Review of Abstractions
  • Machine Language

7

slide-8
SLIDE 8

Microarchitecture Circuits ISA

LE LEVELS LS O OF T TRANSFORMATION

Problem

8

Algorithm Program

Hardware Software

Transistors

slide-9
SLIDE 9

What is Computer Architecture

  • ISA (Instruction Set Architecture)
  • Agreed upon interface between software and

hardware

  • SW/compiler assumes, HW promises
  • What the software writer needs to know to write and

debug system/user programs

  • Microarchitecture
  • Specific implementation of an ISA
  • Not visible to the software
  • Microprocessor
  • ISA, uarch, circuits
  • “Architecture” = ISA + microarchitecture

9

Microarchitecture Circuits ISA Problem Algorithm Program Transistors

slide-10
SLIDE 10

How to interpret zeros and ones?

  • At the lowest level, all the machine sees are zeros and ones
  • How to interpret this strings of zeros and ones?
  • ISA determines how to interpret the software instructions
  • So that hardware can understand those
  • Each bit in the instruction means different things

0110 0000 0000 0010 à What does it mean?

10

slide-11
SLIDE 11

x86 format

11

slide-12
SLIDE 12

ARM

12

slide-13
SLIDE 13

So what language we can use that resembles the machine language?

  • 0110 0000 0000 0010 à What does it mean?
  • x += y à “Higher-level” language: C
  • add %rbx, %rax à Assembly: X86-64
  • 60 03 à Machine code

Microarchitecture Circuits ISA Problem Algorithm Program Transistors

13

slide-14
SLIDE 14

Summary

  • Microarchitecture
  • Specific implementation of an ISA
  • Not visible to the software
  • Microprocessor
  • ISA, uarch, circuits
  • “Architecture” = ISA + microarchitecture
  • Example ISAs:
  • Intel: x86, x86-64
  • ARM: Used in almost all mobile phones
  • Code Forms:
  • Machine Code: The byte-level programs that a processor executes
  • Assembly Code: A text representation of machine code

14

slide-15
SLIDE 15

AG AGENDA

  • Logistics
  • Review of Abstractions
  • Machine Language

15

slide-16
SLIDE 16

Assembly/Machine Code View

Programmer-Visible State

  • PC: Program counter
  • Address of next instruction
  • Called “RIP” (x86-64)
  • Register file
  • Heavily used program data
  • Condition codes
  • Store status information about most

recent arithmetic or logical operation

  • Used for conditional branching

CPU PC Registers Memory

Code Data Stack Addresses Data Instructions

Condition Codes

  • Memory
  • Byte addressable array
  • Code and user data
  • Stack to support procedures

Why do we need PC?

16

slide-17
SLIDE 17

The Von Neumann Model/Architecture

  • Also called stored program computer (instructions in memory). Two key

properties:

  • Stored program
  • Instructions stored in a linear memory array
  • Memory is unified between instructions and data
  • The interpretation of a stored value depends on the control signals
  • Sequential instruction processing
  • One instruction processed (fetched, executed, and completed) at a time
  • Program counter (instruction pointer) identifies the current instr.
  • Program counter is advanced sequentially except for control transfer instructions

17

slide-18
SLIDE 18

A Sample Program

PC will store the address of the next instruction

0000000000400595 <sumstore>: 400595: 53 push %rbx 400596: 48 89 d3 mov %rdx,%rbx 400599: e8 f2 ff ff ff callq 400590 <plus> 40059e: 48 89 03 mov %rax,(%rbx) 4005a1: 5b pop %rbx 4005a2: c3 retq

Address of the function sumstore

18

slide-19
SLIDE 19

Assembly Language

  • Operations
  • Data types
  • Registers
  • Addressing modes
  • Branches

19

slide-20
SLIDE 20
  • Arithmetic
  • Perform arithmetic function on register or memory data
  • Data Transfer
  • Transfer data between memory and register
  • Load data from memory into register
  • Store register data into memory
  • Control
  • Transfer control
  • Unconditional jumps to/from procedures
  • Conditional branches

Assembly Language: Operations

20

slide-21
SLIDE 21

Instructions

  • movq Source, Dest:
  • movq $42, (%rbx)
  • memory[rbx] ß 42
  • subq %rax, %rbx
  • rbx ß rbx – rax
  • addq 0x1000, %rax
  • rax ß rax + memory[0x1000]
  • addq $0x1000, %rax
  • rax ß rax + 0x1000

21

slide-22
SLIDE 22

Assembly Language

  • Operations
  • Data types
  • Registers
  • Addressing modes
  • Branches

22

slide-23
SLIDE 23

Data Types

  • movq $42, (%rbx)
  • memory[rbx] ß 42
  • q indicates length (8 bytes)
  • l: 4; w: 2; b: 1
  • movl, movw, movb

Type Specifier Bytes addressed BYTE 1 WORD 2 DWORD 4 QWORD 8

23

slide-24
SLIDE 24

Assembly Language

  • Operations
  • Data types
  • Registers
  • Addressing modes
  • Branches

24

slide-25
SLIDE 25

%rsp

x86-64 Integer Registers

%eax %ebx %ecx %edx %esi %edi %esp %ebp %r8d %r9d %r10d %r11d %r12d %r13d %r14d %r15d

%r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp

25

slide-26
SLIDE 26

Assembly Language

  • Operations
  • Data types
  • Registers
  • Addressing modes
  • Branches

26

slide-27
SLIDE 27

Addressing Modes

  • Most General Form

D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D]

  • D:

Constant “displacement” 1, 2, or 4 bytes

  • Rb: Base register: Any of 16 integer registers
  • Ri:

Index register: Any, except for %rsp

  • S:

Scale: 1, 2, 4, or 8

  • Special Cases

(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]]

27

slide-28
SLIDE 28

Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)

Carnegie Mellon

Address Computation Examples

%rdx 0xf000 %rcx 0x0100

28

slide-29
SLIDE 29

Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)

Carnegie Mellon

Address Computation Examples

Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2) %rdx 0xf000 %rcx 0x0100

29

slide-30
SLIDE 30

Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)

Carnegie Mellon

Address Computation Examples

Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0x80(,%rdx,2) %rdx 0xf000 %rcx 0x0100

30

slide-31
SLIDE 31

Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)

Carnegie Mellon

Address Computation Examples

Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400 0x80(,%rdx,2) %rdx 0xf000 %rcx 0x0100

31

slide-32
SLIDE 32

Expression Address Computation Address 0x8(%rdx) (%rdx,%rcx) (%rdx,%rcx,4) 0x80(,%rdx,2)

Carnegie Mellon

Address Computation Examples

Expression Address Computation Address 0x8(%rdx) 0xf000 + 0x8 0xf008 (%rdx,%rcx) 0xf000 + 0x100 0xf100 (%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400 0x80(,%rdx,2) 2*0xf000 + 0x80 0x1e080 %rdx 0xf000 %rcx 0x0100

32

slide-33
SLIDE 33

Why so many modes?

  • Advantage of more addressing modes:
  • Enables better mapping of high-level constructs to the machine: some

accesses are better expressed with a different mode à reduced number of instructions and code size

  • Think array accesses
  • Disadvantage:
  • More work for the compiler
  • More work for the microarchitect
  • (Remember that these addressing modes need to be supported by

the ISAs and implemented in the microarchitecture)

33

slide-34
SLIDE 34

Assembly Language

  • Operations
  • Data types
  • Registers
  • Addressing modes
  • Branches

34

slide-35
SLIDE 35

Carnegie Mellon

Branches

  • Control: Condition codes
  • Conditional branches
  • Loops

35

slide-36
SLIDE 36

Carnegie Mellon

Review: Processor State (x86-64)

  • Information about currently

executing program

  • Temporary data

( %rax, … )

  • Location of runtime stack

( %rsp )

  • Location of current code control point

( %rip, … )

  • Status of recent tests

( CF, ZF, SF, OF )

%rip

Registers Current stack top Instruction pointer CF ZF SF OF Condition codes

%rsp %r8 %r9 %r10 %r11 %r12 %r13 %r14 %r15 %rax %rbx %rcx %rdx %rsi %rdi %rbp

36

slide-37
SLIDE 37

Carnegie Mellon

Condition Codes

  • Single bit registers
  • CF

Carry Flag (for unsigned)

  • SF

Sign Flag (for signed)

  • ZF

Zero Flag

  • OF

Overflow Flag (for signed)

  • Implicitly set (think of it as side effect) by arithmetic operations

Example: addq Src,Dest ↔ t = a+b CF set if carry out from most significant bit (unsigned overflow) ZF set if t == 0 SF set if t < 0 (as signed) OF set if two’s-complement (signed) overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0)

37

slide-38
SLIDE 38

Carnegie Mellon

Condition Codes (Explicit Setting: Compare)

  • Explicit Setting by Compare Instruction
  • cmpq Src2, Src1
  • cmpq b,a like computing a-b without setting destination
  • CF set if carry out from most significant bit (used for unsigned comparisons)
  • ZF set if a == b
  • SF set if (a-b) < 0 (as signed)
  • OF set if two’s-complement (signed) overflow

(a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0)

38

slide-39
SLIDE 39

Carnegie Mellon

Branches

  • Control: Condition codes
  • Conditional branches
  • Loops

39

slide-40
SLIDE 40

Carnegie Mellon

Jumping

  • jX Instructions
  • Jump to different part of code depending on condition codes

jX Description jmp Unconditional je Equal / Zero jne Not Equal / Not Zero jg Greater (Signed) jge Greater or Equal (Signed) jl Less (Signed) jle Less or Equal (Signed)

40

slide-41
SLIDE 41

Carnegie Mellon

Conditional Branch Example

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }

absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret

Register Use(s) %rdi Argument x %rsi Argument y %rax Return value

How should we go from conditional branch to assembly code?

41

slide-42
SLIDE 42

Carnegie Mellon

Step 1: Expressing with Goto Code

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }

  • Test the condition
  • Label the else part
  • Place the if part
  • Jump to position designated by label

long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }

42

slide-43
SLIDE 43

Carnegie Mellon

Step 1: Expressing with Goto Code

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }

  • Test the condition
  • Label the else part
  • Place the if part
  • Jump to position designated by label

long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }

43

slide-44
SLIDE 44

Carnegie Mellon

Step 1: Expressing with Goto Code

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }

  • Test the condition
  • Label the else part
  • Place the if part
  • Jump to position designated by label

long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }

44

slide-45
SLIDE 45

Carnegie Mellon

Step 1: Expressing with Goto Code

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; }

  • Test the condition
  • Label the else part
  • Place the if part
  • Jump to position designated by label

long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; }

45

slide-46
SLIDE 46

Carnegie Mellon

Step 2: Convert Goto Code to Assembly

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret

46

slide-47
SLIDE 47

Carnegie Mellon

Expressing with Goto Code

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret

47

slide-48
SLIDE 48

Carnegie Mellon

Expressing with Goto Code

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret

48

slide-49
SLIDE 49

Carnegie Mellon

Expressing with Goto Code

long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j(long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } absdiff: cmpq %rsi, %rdi # x-y jle .L4 movq %rdi, %rax subq %rsi, %rax # x-y ret .L4: # x <= y movq %rsi, %rax subq %rdi, %rax # y-x ret

49

slide-50
SLIDE 50

Carnegie Mellon

C Code

val = Test ? Then_Expr : Else_Expr;

Goto Version

ntest = !Test; if (ntest) goto Else; val = Then_Expr; goto Done; Else: val = Else_Expr; Done: . . .

Summary: Conditional Branch

  • Create separate code regions

for then & else expressions

  • Execute appropriate one

val = x>y ? x-y : y-x;

50

slide-51
SLIDE 51

Carnegie Mellon

Branches

  • Control: Condition codes
  • Conditional branches
  • Loops
  • Do-while
  • While
  • for

51

slide-52
SLIDE 52

Carnegie Mellon

C Code

long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; }

  • 1. “Do-While” Loop Example

Step 1: Expressing with Goto Code

52

slide-53
SLIDE 53

Carnegie Mellon

C Code

long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; }

Goto Version

long pcount_goto (unsigned long x) { long result = 0; .loop result += x & 0x1; x >>= 1; if(x) goto loop; return result; }

  • 1. “Do-While” Loop Example

Declare the goto label where the body starts Step 1: Expressing with Goto Code

53

slide-54
SLIDE 54

Carnegie Mellon

C Code

long pcount_do (unsigned long x) { long result = 0; do { result += x & 0x1; x >>= 1; } while (x); return result; }

Goto Version

long pcount_goto (unsigned long x) { long result = 0; .loop result += x & 0x1; x >>= 1; if(x) goto loop; return result; }

  • 1. “Do-While” Loop Example

Use conditional branch to either continue looping or to exit loop Step 1: Expressing with Goto Code

54

slide-55
SLIDE 55

Carnegie Mellon

Goto Version

Step 2: Convert Goto Code to Assembly

movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result

55

slide-56
SLIDE 56

Carnegie Mellon

Goto Version

  • 1. “Do-While” Loop Example

movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result

56

slide-57
SLIDE 57

Carnegie Mellon

Goto Version

  • 1. “Do-While” Loop Example

movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result

57

slide-58
SLIDE 58

Carnegie Mellon

Goto Version

  • 1. “Do-While” Loop Example

movl $0, %eax # result = 0 .L2: # loop: movq %rdi, %rdx andl $1, %edx # t = x & 0x1 addq %rdx, %rax # result += t shrq %rdi # x >>= 1 jne .L2 # if (x) goto loop ret long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0x1; x >>= 1; if(x) goto loop; return result; } Register Use(s) %rdi Argument x %rax result

58

slide-59
SLIDE 59

Carnegie Mellon

C Code do Body while (Test); Goto Version loop: Body if (Test) goto loop

  • 1. General “Do-While” Translation
  • Body:

{ Statement1; Statement2; … Statementn; }

59

slide-60
SLIDE 60

Carnegie Mellon

Branches

  • Control: Condition codes
  • Conditional branches
  • Loops
  • Do-while
  • While
  • for

60

slide-61
SLIDE 61

Carnegie Mellon

While version while (Test) Body Do-While Version if (!Test) goto done; do Body while(Test); done:

  • 2. General “While” Translation

Goto Version if (!Test) goto done; loop: Body if (Test) goto loop; done:

61

slide-62
SLIDE 62

Carnegie Mellon

Branches

  • Control: Condition codes
  • Conditional branches
  • Loops
  • Do-while
  • While
  • for

62

slide-63
SLIDE 63

Carnegie Mellon

  • 3. “For” Loop Form

for (Init; Test; Update ) Body General Form

#define WSIZE 8*sizeof(int) long pcount_for (unsigned long x) { size_t i; long result = 0; for (i = 0; i < WSIZE; i++) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

In Init Te Test Up Update Bod Body

63

slide-64
SLIDE 64

Carnegie Mellon

  • 3. “For” Loop à While Loop

for (Init; Test; Update ) Body For Version Init; while (Test ) { Body Update; } While Version

64

slide-65
SLIDE 65

Carnegie Mel

  • 3. For-While Conversion

long pcount_for_while (unsigned long x) { size_t i; long result = 0; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test Update Body Init; while (Test ) { Body Update; }

65

slide-66
SLIDE 66

Carnegie Mel

  • 3. For-While Conversion

long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

In Init

Test Update Body Init; while (Test ) { Body Update; }

66

slide-67
SLIDE 67

Carnegie Mel

  • 3. For-While Conversion

long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init

Te Test

Update Body Init; while (Test ) { Body Update; }

67

slide-68
SLIDE 68

Carnegie Mel

  • 3. For-While Conversion

long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { unsigned bit = (x >> i) & 0x1; result += bit; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test Update

Bo Body

Init; while (Test ) { Body Update; }

68

slide-69
SLIDE 69

Carnegie Mel

  • 3. For-While Conversion

long pcount_for_while (unsigned long x) { size_t i; long result = 0; i = 0; while (i < WSIZE) { unsigned bit = (x >> i) & 0x1; result += bit; i++; } return result; } i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test

Upda Update

Body Init; while (Test ) { Body Update; }

69

slide-70
SLIDE 70

Carnegie Mellon

3’: “For” Loop à Do-While Loop

for (Init; Test; Update ) Body For Version Init; Loop: Body Update; Test; goto Loop; Do-While Version

70

slide-71
SLIDE 71

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test Update Body

71

slide-72
SLIDE 72

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

In Init

Test Update Body

72

slide-73
SLIDE 73

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test Update Body

73

slide-74
SLIDE 74

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test Update

Body

74

slide-75
SLIDE 75

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test

Update

Body

75

slide-76
SLIDE 76

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init

Te Test

Update Body

76

slide-77
SLIDE 77

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; } i++; if (i < WSIZE) goto loop; done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test Update Body

77

slide-78
SLIDE 78

Carnegie Mellon

3’: “For” Loop Do-While Conversion

long pcount_for_goto_dw (unsigned long x) { size_t i; long result = 0; i = 0; loop: { unsigned bit = (x >> i) & 0x1; result += bit; i++; } if (i < WSIZE) goto loop; done: return result; }

Init; Loop: Body Update; Test; goto Loop;

i = 0 i < WSIZE i++ { unsigned bit = (x >> i) & 0x1; result += bit; }

Init Test Update Body

78

slide-79
SLIDE 79

Carnegie Mellon

Machine Language Summary

  • A text representation of machine code
  • Three types of operations
  • Arithmetic
  • Data Transfer
  • Control
  • Can represent high-level branches
  • Do-While, While, for

79

slide-80
SLIDE 80

Samira Khan

University of Virginia Feb 2, 2017

Machine Language

CS 3330

slide-81
SLIDE 81

Carnegie Mellon

C Code

long pcount_while (unsigned long x) { long result = 0; while (x) { result += x & 0x1; x >>= 1; } return result; }

Do-While Version

long pcount_goto_dw (unsigned long x) { long result = 0; if (!x) goto done; loop: result += x & 0x1; x >>= 1; if(x) goto loop; done: return result; }

While Loop Example

Initial conditional guards entrance to loop

81