Understanding Computer Systems: Machine-Level Representation of Programs

Download Presenatation
csc 2400 computer systems n.w
1 / 66
Embed
Share

Explore the journey from high-level languages like Java and C to assembly language and machine language (IA-32). Dive into compilation stages and learn how to build and run executables. Delve into compiling C code into assembly code and understand the relationship between C and assembly language. Uncover the significance of knowing computer architecture and memory addressing using unsigned binary integers.

  • Computer Systems
  • Machine-Level Representation
  • Programming Languages
  • Assembly Language
  • Computer Architecture

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. CSC 2400: Computer Systems Towards the Hardware: Machine-Level Representation of Programs

  2. Towards the Hardware High-level language (Java) High-level language (C) assembly language machine language (IA-32)

  3. Compilation Stages count = 0; while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2; } 00000000000000000000000000000000 mov ECX, 0 00000000000000000000000000000000 .loop: 01100011010101110110001101010111 cmp jle EDX, 1 .endloop 00101011101011010010101110101101 11010001110111011101000111011101 add ECX, 1 00101110100111000010111010011100 mov and je mov add add add EAX, EDX EAX, 1 .else EAX, EDX EDX, EAX EDX, EAX EDX, 1 11010010001111001101001000111100 11010000011111011101000001111101 11010011101010011101001110101001 11010000111111101101000011111110 11010001010000101101000101000010 jmp .endif .else: sar EDX, 1 .endif: jmp .loop .endloop: High level language Assembly language Machine language

  4. Building and Running To build an executable $ gcc program.c o program Result Complete executable binary file Machine language 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 0 1 0 1 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 To run: $ ./program

  5. Compiling Into Assembly C Code (code.c) Generated Assembly (code.s) int sum(int x, int y) { int t = x+y; accum += t; return t; } sum: push mov mov mov add pop ret ebp ebp, esp edx, DWORD PTR [ebp+12] eax, DWORD PTR [ebp+8] eax, edx ebp Obtained with command gcc masm=intel -S code.c Produces file code.s

  6. Lecture Goals Help you learn Relationship between C and assembly language IA-32 assembly language through an example Why do you need to know this?

  7. Computer Architecture Background

  8. Main Memory Memory addresses are defined using unsigned binary integers

  9. CPU and Memory Example of a dual core architecture Registers are fastest-speed temporary storage Cache acts as a buffer between memory and registers Memory The only large storage area that the CPU can access directly Hence, any program executing must be in memory 0000000000000000 10100111 11010010 11000010 00101001 ... 0000000000000001 0000000000000010 0000000000000011 . . .

  10. Central Processing Unit(CPU) AX Machine Instr1 Machine Instr2 Machine Instr3 Machine Instr4 BX Arithmetic Logic Unit (ALU) CX DX Machine Instructions move data in and out of registers manipulate the register contents Registers act as temporary variables

  11. Remember This? Part of the ALU

  12. Central Processing Unit (CPU) Runs the loop Fetch-Decode-Execute Decode Instruction Fetch Next Instruction Execute Instruction Execute Instruction Execute Instruction START HALT START Fetch the next instruction from memory Where from? A CPU register called the Instruction Pointer (EIP) holds the address of the instruction to be fetched next Decode the instruction and fetch the operands Execute the instruction and store the result -

  13. Control Unit: Instruction Decoder Determines what operations need to take place Translate the machine-language instruction Control what operations are done on what data E.g., control what data are fed to the ALU E.g., enable the ALU to do multiplication or addition E.g., read from a particular address in memory src1 src2 operation flag/carry ALU dst

  14. Intel Architecture, 32-bit (IA-32) CPU Registers Memory Registers EIP Addresses EAX EBX ECX EDX ESI EDI ESP EBP TEXT DATA Data HEAP Instructions STACK Condition Codes EFLAGS CF ZF SF OF EIP ESP, EBP Reserved for special use EAX Always contains return value Instruction Pointer

  15. Central Processing Unit(CPU) AX Machine Instr1 Machine Instr2 Machine Instr3 Machine Instr4 BX Arithmetic Logic Unit (ALU) CX DX Machine Instructions move data in and out of registers manipulate the register contents Registers act as temporary variables

  16. Registers Small amount of storage on the CPU Can be accessed more quickly than main memory Instructions move data in and out of registers Loading registers from main memory Storing registers to main memory Instructions manipulate the register contents Registers essentially act as temporary variables For efficient manipulation of the data Registers are the top of the memory hierarchy Ahead of main memory, disk, tape,

  17. C Code vs. Assembly Code

  18. Kinds of Instructions Reading and writing data count = 0 n count = 0; while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2; } Arithmetic and logic operations Increment: count++ Multiply: n * 3 Divide: n/2 Logical AND: n & 1 Checking results of comparisons Is (n > 1) true or false? Is (n & 1) non-zero or zero? Changing the flow of control To the end of the while loop (if n 1 ) Back to the beginning of the loop To the else clause (if n & 1 is 0)

  19. Variables in Registers Registers count = 0; while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2; } count ECX n EDX mov ECX, DWORD PTR count mov EDX, DWORD PTR n

  20. Immediate and Register Addressing count ECX n EDX count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } mov ECX, 0 add ECX, 1 written to a register

  21. General Syntax op Dest, Src Perform operation op on Src and Dest Save result in Dest

  22. Immediate and Register Addressing count ECX n EDX count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } mov and EAX, EDX EAX, 1 Computing intermediate value in register EAX

  23. Immediate and Register Addressing count ECX n EDX count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } mov add add add EAX, EDX EDX, EAX EDX, EAX EDX, 1 Adding n twice is cheaper than multiplication!

  24. Immediate and Register Addressing count ECX n EDX count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } sar EDX, 1 Shifting right by 1 bit is cheaper than division!

  25. Changing Program Flow Cannot simply run next instruction Check result of a previous operation Jump to appropriate next instruction count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } Jump instructions Load new address in instruction pointer Example jump instructions Jump unconditionally (e.g., } ) Jump if zero (e.g., n & 1 ) Jump if greater/less (e.g., n > 1 )

  26. Jump Instructions Jump depends on the result of previous arithmetic instruction Registers EAX EBX ECX EDX ESI EDI ESP EBP EIP Jump jmp je jne jg jge jl jle Description Unconditional Equal / Zero Condition Codes EFLAGS CF ZF SF OF

  27. Remember This? Part of the ALU SF CF OF

  28. Jump to different part of code depending on condition codes jX jmp je jne js jns jg jge jl jle ja jb Condition 1 ZF ~ZF SF ~SF ~(SF^OF)&~ZF ~(SF^OF) (SF^OF) (SF^OF)|ZF ~CF&~ZF CF Description Unconditional Equal / Zero Not Equal / Not Zero Negative Nonnegative Greater (Signed) Greater or Equal (Signed) Less (Signed) Less or Equal (Signed) Above (unsigned) Below (unsigned)

  29. Conditional and Unconditional Jumps Comparison cmp compares two integers Done by subtracting the first number from the second Discards the results, but sets flags as a side effect: cmp EDX, 1 (computes EDX 1) jle .endloop (checks whether result was 0 or negative) Logical operation and compares two integers: and EAX, 1 (bit-wise AND of EAX with 1) je .else (checks whether result was 0) Also, can do an unconditional branch jmp jmp .endif jmp .loop

  30. Jump and Labels: While Loop count ECX n EDX .loop: cmp jle EDX, 1 .endloop while (n>1) { Checking if EDX is less than or equal to 1. } jmp .loop .endloop:

  31. count ECX n Jump and Labels: While Loop EDX mov ECX, 0 .loop: cmp jle EDX, 1 .endloop count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } add ECX, 1 mov and je mov add add add EAX, EDX EAX, 1 .else EAX, EDX EDX, EAX EDX, EAX EDX, 1 jmp .endif .else: sar EDX, 1 .endif: jmp .loop .endloop:

  32. count ECX n Jump and Labels: If-Then-Else EDX mov and je EAX, EDX EAX, 1 .else if (n&1) ... else ... then block jmp .endif .else: else block .endif:

  33. count ECX n Jump and Labels: If-Then-Else EDX mov ECX, 0 .loop: cmp jle EDX, 1 .endloop count=0; while(n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } add ECX, 1 mov and je mov add add add EAX, EDX EAX, 1 .else EAX, EDX EDX, EAX EDX, EAX EDX, 1 then block jmp .endif .else: sar EDX, 1 else block .endif: jmp .loop .endloop:

  34. count ECX n Code More Efficient EDX mov ECX, 0 .loop: cmp jle EDX, 1 .endloop count=0; while(n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } add ECX, 1 mov and je mov add add add EAX, EDX EAX, 1 .else EAX, EDX EDX, EAX EDX, EAX EDX, 1 jmp .endif .else: sar EDX, 1 .endif: Replace with jmp loop jmp .loop .endloop:

  35. count ECX n Complete Example EDX mov ECX, 0 .loop: cmp jle EDX, 1 .endloop count=0; while (n>1) { count++; if (n&1) n = n*3+1; else n = n/2; } add ECX, 1 mov and je mov add add add EAX, EDX EAX, 1 .else EAX, EDX EDX, EAX EDX, EAX EDX, 1 jmp .endif .else: sar EDX, 1 .endif: jmp .loop .endloop:

  36. Reading IA-32 Assembly Language Referring to a register: EAX, eax, EBX, ebx, etc. Result stored in the first argument E.g. mov EAX, EDX moves EDX into EAX E.g., add ECX, 1 increments register ECX Comment: pound sign ( # ) E.g., # Purpose: Convert lower to upper case

  37. X86 C Example int Example(int x){ ??? } Write comments push EBP mov EBP, ESP mov ECX, [EBP+8] xor EAX, EAX xor EDX, EDX cmp EDX, ECX jge L2 add EAX, EDX inc EDX cmp EDX, ECX jl L1 mov ESP,EBP pop EBP ret L1: L2: ECX = x EAX = EDX = if goto L2 EAX = EDX = if goto L1 L1: L2:

  38. Name the Variables EAX result, EDX i int Example(int x){ ??? } push EBP mov EBP, ESP mov ECX, [EBP+8] xor EAX, EAX xor EDX, EDX cmp EDX, ECX jge L2 add EAX, EDX inc EDX cmp EDX, ECX jl L1 mov ESP,EBP pop EBP ret L1: L2: ECX = x result = i = if goto L2 result = i = if goto L1 L1: L2:

  39. Identify the Loop result = 0; i = 0; if (i >= x) goto L2; L1:result += i; i++; if (i < x) goto L1; L2:

  40. Identify the Loop result = 0; i = 0; if (i >= x) goto L2; do { result += i; i++; } while (i < x); L2: result = 0; i = 0; if (i >= x) goto L2; result = 0; i = 0; while (i < x){ result += i; i++; } L1:result += i; i++; if (i < x) goto L1; L2: result = 0; for (i = 0; i < x; i++) result += i;

  41. C Code int Example(int x) { int result = 0; int i; result += i; for (i = 0; i < x; i++) } return result;

  42. Exercise int F(int x, int y){ ??? } push EBP mov EBP,ESP mov ECX, [EBP+8] mov EDX, [EBP+12] xor EAX, EAX cmp ECX, EDX jle .L1 .L2: dec ECX inc EDX inc EAX cmp ECX,EDX jg .L2 .L1: inc EAX mov ESP,EBP pop EBP ret # ECX = x # EDX = y .L2: .L1:

  43. C Code int F(int x, int y)

  44. 46

  45. Instructions to Recognize

  46. Arithmetic Instructions (1) Two Operand Instructions ADD Dest, SrcDest = Dest + Src SUB Dest, SrcDest = Dest - Src MULDest, SrcDest = Dest * Src SAL Dest, Src Dest = Dest << Src SAR Dest, Src Dest = Dest >> Src SHR Dest, Src Dest = Dest >> Src XOR Dest, Src Dest = Dest ^ Src AND Dest, Src Dest = Dest & Src OR Dest, Src Dest = Dest | Src Arithmetic Logical

  47. Arithmetic Instructions (2) One Operand Instructions INC Dest DEC Dest NEGDest NOT Dest Dest = Dest + 1 Dest = Dest 1 Dest = -Dest Dest = ~Dest

  48. Compare and Test Instructions CMP Dest, Src Compute Dest - Src without setting Dest TEST Dest, Src Compute Dest & Src without setting Dest

  49. Jump Instructions Jump depending on the result of the previous arithmetic instruction: Jump Description jmp Unconditional je Equal / Zero jne Not Equal / Not Zero js Negative jns Nonnegative jg Greater (Signed) jge Greater or Equal (Signed) jl Less (Signed) jle Less or Equal (Signed) ja Above (unsigned) jb Below (unsigned)

  50. Loading and Storing Data Memory Addressing Modes

More Related Content