Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Architecture

Similar presentations


Presentation on theme: "Computer Architecture"— Presentation transcript:

1 Computer Architecture
x86 Programming Computer Architecture

2 * Where k is an int variable
CMP Instruction CMP Instruction compares two operands Internally comparison is performed via subtraction One operand has to be a register or constant (8, 16, or 32 bits) Other operand can be a variable (memory address) or constant Sets flags as the SUB instruction Changes CF, OF, SF, PF, and ZF Operands are not modified. Instruction Result cmpb %BL, %AL FLAGS(%AL - %BL) cmpl $8, %EAX FLAGS(%EAX – 8) cmpl k, %EAX FLAGS(%eax – k) cmpl $8, k FALGS(k – 8) * Where k is an int variable

3 JMPcc Instruction JMPcc is a family of instruction where ‘cc’ stands for condition code Performs a jump or a branch if a condition is met Conditions are flag values set by earlier calls to CMP There are 100s of flavors of JMPcc instruction For different combination of conditions (FLAG values) For different JMP sizes 8, 16, or 32-bit offset values to be added to IP We will cover a few in class. Refer to manual for details.

4 JMPcc Instruction CMP a, b A compare must be performed first!
Flags Used JMP Taken IF JE label1 ZF=1 a == b JNE label1 ZF=0 a != b JG label1 ZF=0, SF=CF a > b JL label1 SF!=CF a < b JLE label1 ZF=1, SF!=CF a <= b JGE label1 SF=OF a >= b

5 Example Usage Convert the following Java code to assembly
public static main(String[] args) { int a = 10, b = 20; int c = 0; if (a > b) { c = a + b; } else { c = a – b; .text /* Every thing below goes in Code Segment */ .global _start /* Global entry point into program */ _start: /* Indicate where program starts */ movl a, %eax /* eax = a */ cmpl b, %eax /* Set Flags */ JG ifPart /* a > b */ subl b, %eax /* eax -= b */ jmp endIF /* Done with else */ ifPart: addl b, %eax /* eax += b */ endIF: movl %eax, c /* c = eax */

6 Example Usage (Contd.) Convert the following Java code to assembly
public static main(String[] args) { int a = 10, b = 20; int c = 0; if (a > b) { c = a + b; } else { c = a – b; /* Continued from previous slide*/ /* exit*/ movl $1, %eax movl $0, %ebx int $0x80 .data /* Everything below goes in data segment */ a: .int 10 b: .int 20 c: .int 0


Download ppt "Computer Architecture"

Similar presentations


Ads by Google