CSC 2400: Computer Systems
Towards the Hardware:
Machine-Level Representation
- f Programs
CSC 2400: Computer Systems Towards the Hardware: Machine-Level - - PowerPoint PPT Presentation
CSC 2400: Computer Systems Towards the Hardware: Machine-Level Representation of Programs Towards the Hardware High-level language (Java) High-level language (C) assembly language machine language (IA-32) Compilation Stages
count = 0; while (n > 1) { count++; if (n & 1) n = n*3 + 1; else n = n/2; }
00000000000000000000000000000000 00000000000000000000000000000000 01100011010101110110001101010111 00101011101011010010101110101101 11010001110111011101000111011101 00101110100111000010111010011100 11010010001111001101001000111100 11010000011111011101000001111101 11010011101010011101001110101001 11010000111111101101000011111110 11010001010000101101000101000010
mov EAX, EDX and EAX, 1 je .else jmp .endif .else: .endif: sar EDX, 1 mov EAX, EDX add EDX, EAX add EDX, EAX add EDX, 1 add ECX, 1 .loop: cmp EDX, 1 jle .endloop jmp .loop .endloop: mov ECX, 0
! Complete executable binary file ! Machine language
$ gcc program.c –o program $ ./program 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
int sum(int x, int y) { int t = x+y; accum += t; return t; }
sum: push ebp mov ebp, esp mov edx, DWORD PTR [ebp+12] mov eax, DWORD PTR [ebp+8] add eax, edx pop ebp ret
0000000000000000
10100111 11010010 11000010 00101001
CPU can access directly
be in memory
. . .
0000000000000001 0000000000000010 0000000000000011 . . .
Registers are fastest-speed temporary storage Cache acts as a buffer between memory and registers
Arithmetic Logic Unit (ALU)
AX BX CX DX
Machine Instr1 Machine Instr2 Machine Instr3 Machine Instr4
q Machine Instructions
! move data in and out of registers ! manipulate the register contents
q Registers act as temporary variables
Fetch Next Instruction
START Execute Instruction
Execute Instruction
Execute Instruction
HALT Decode Instruction START
q Fetch the next instruction from memory
(EIP) holds the address of the instruction to be fetched next
q Decode the instruction and fetch the operands q Execute the instruction and store the result
HEAP STACK TEXT DATA Addresses Instructions Data EIP Registers Condition Codes EAX EBX ECX EDX ESI EDI ESP EBP CF ZF SF OF CPU Registers Memory EIP Instruction Pointer ESP, EBP Reserved for special use EAX Always contains return value
EFLAGS
Arithmetic Logic Unit (ALU)
AX BX CX DX
Machine Instr1 Machine Instr2 Machine Instr3 Machine Instr4
q Machine Instructions
! move data in and out of registers ! manipulate the register contents
q Registers act as temporary variables
! count = 0 ! n
! Increment: count++ ! Multiply: n * 3 ! Divide: n/2 ! Logical AND: n & 1
! Is (n > 1) true or false? ! Is (n & 1) non-zero or zero?
! 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)
mov ECX, 0 add ECX, 1
count ECX n EDX
mov EAX, EDX and EAX, 1
count ECX n EDX
mov EAX, EDX add EDX, EAX add EDX, EAX add EDX, 1
count ECX n EDX
sar EDX, 1
count ECX n EDX
! Check result of a previous operation ! Jump to appropriate next instruction
! Load new address in instruction pointer
! Jump unconditionally (e.g., “}”) ! Jump if zero (e.g., “n & 1”) ! Jump if greater/less (e.g., “n > 1”)
Jump Description
jmp Unconditional je Equal / Zero jne jg jge jl jle EIP Registers Condition Codes EAX EBX ECX EDX ESI EDI ESP EBP CF ZF SF OF EFLAGS
jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned) ! Jump to different part of code depending on condition codes
! 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)
– and EAX, 1 (bit-wise AND of EAX with 1) – je .else (checks whether result was 0)
– jmp .endif – jmp .loop
.loop: cmp EDX, 1 jle .endloop … jmp .loop .endloop:
count ECX n EDX
mov EAX, EDX and EAX, 1 je .else jmp .endif .else: .endif: sar EDX, 1 mov EAX, EDX add EDX, EAX add EDX, EAX add EDX, 1 add ECX, 1 .loop: cmp EDX, 1 jle .endloop jmp .loop .endloop: mov ECX, 0
count ECX n EDX
mov EAX, EDX and EAX, 1 je .else jmp .endif .else: .endif:
count ECX n EDX
mov EAX, EDX and EAX, 1 je .else jmp .endif .else: .endif: sar EDX, 1 mov EAX, EDX add EDX, EAX add EDX, EAX add EDX, 1 add ECX, 1 .loop: cmp EDX, 1 jle .endloop jmp .loop .endloop: mov ECX, 0
count ECX n EDX
mov EAX, EDX and EAX, 1 je .else jmp .endif .else: .endif: sar EDX, 1 mov EAX, EDX add EDX, EAX add EDX, EAX add EDX, 1 add ECX, 1 .loop: cmp EDX, 1 jle .endloop jmp .loop .endloop: mov ECX, 0
count ECX n EDX
mov EAX, EDX and EAX, 1 je .else jmp .endif .else: .endif: sar EDX, 1 mov EAX, EDX add EDX, EAX add EDX, EAX add EDX, 1 add ECX, 1 .loop: cmp EDX, 1 jle .endloop jmp .loop .endloop: mov ECX, 0
count ECX n EDX
push EBP mov EBP, ESP mov ECX, [EBP+8] xor EAX, EAX xor EDX, EDX cmp EDX, ECX jge L2 L1: add EAX, EDX inc EDX cmp EDX, ECX jl L1 L2: mov ESP,EBP pop EBP ret ECX = x EAX = EDX = if goto L2 EAX = EDX = if goto L1
L2: L1:
push EBP mov EBP, ESP mov ECX, [EBP+8] xor EAX, EAX xor EDX, EDX cmp EDX, ECX jge L2 L1: add EAX, EDX inc EDX cmp EDX, ECX jl L1 L2: mov ESP,EBP pop EBP ret ECX = x result = i = if goto L2 result = i = if goto L1 L2: L1:
result = 0; i = 0; if (i >= x) goto L2; L1:result += i; i++; if (i < x) goto L1; L2:
result = 0; i = 0; if (i >= x) goto L2; L1:result += i; i++; if (i < x) goto L1; L2: result = 0; i = 0; if (i >= x) goto L2; do { result += i; i++; } while (i < x); L2: result = 0; i = 0; while (i < x){ result += i; i++; } result = 0; for (i = 0; i < x; i++) result += i;
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 .L1: .L2:
46
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)
EAX EBX ECX EDX ESI EDI 31 AX BX CX DX
DI SI AL AH BL CL DL BH CH DH 8 7 15
! Direct memory addressing
! MOV ECX, 10
! MOV ECX, EAX
! MOV ECX, [200]
! MOV ECX, [EAX] ! MOV ECX, [EAX+4] ! MOV ECX, [EAX + ESI*4 + 12]
int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } arith: PUSH EBP MOV EBP,ESP MOV ECX, DWORD PTR [EBP+8] MOV EDX, DWORD PTR [EBP+12] LEA EAX, [EDX+EDX*2] SAL EDX, 4 LEA EAX, [ECX+4+EAX] ADD EDX, ECX ADD EDX, DWORD PTR [EBP+16] IMUL EAX,EDX POP EBP RET Body Setup Finish
int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } x is at address ebp+8 y is at address ebp+12 z is at address ebp+16 To be explained in next lecture MOV ECX, DWORD PTR [EBP+8] ; ECX = MOV EDX, DWORD PTR [EBP+12] ; EDX = LEA EAX, [EDX+EDX*2] ; EAX = SAL EAX, 4 ; EAX = LEA EAX, [ECX+4+EAX] ; EAX = ADD EDX, ECX ; EDX = ADD EDX, DWORD PTR [EBP+16] ; EDX = IMUL EAX,EDX ; EAX =
int arith (int x, int y, int z) { int t1 = x+y; int t2 = z+t1; int t3 = x+4; int t4 = y * 48; int t5 = t3 + t4; int rval = t2 * t5; return rval; } x is at address ebp+8 y is at address ebp+12 z is at address ebp+16 To be explained in next lecture MOV ECX, DWORD PTR [EBP+8] ; ECX = MOV EDX, DWORD PTR [EBP+12] ; EDX = LEA EAX, [EDX+EDX*2] ; EAX = SAL EAX, 4 ; EAX = LEA EAX, [ECX+4+EAX] ; EAX = ADD EDX, ECX ; EDX = ADD EDX, DWORD PTR [EBP+16] ; EDX = IMUL EAX,EDX ; EAX = x y 3y 48y x+4+48y (t5) x+y x+y+z (t2) t2*t5 (return value)
! General move instruction
PUSH EBX # equivalent instructions SUB ESP, 4 MOV [ESP], EBX
POP ECX # equivalent instructions MOV ECX, [ESP] ADD ESP, 4 ESP
ESP
EBX ESP
ESP
ECX