100 likes | 321 Views
ECE 291. Lecture 5: x86 Instructions: Control Flow Constantine D. Polychronopoulos Professor, ECE Office: 463 CSRL. Spring 2000. Branch Instructions. Case Statements. CMP AX, BX JNE Case 2 <Do case 1> JMP Done Case 2: SUB AX, DX CMP AX, BX JNE Case 3
E N D
ECE 291 Lecture 5: x86 Instructions: Control Flow Constantine D. Polychronopoulos Professor, ECE Office: 463 CSRL Spring 2000 ECE 291 -- Spring 2000
Branch Instructions ECE 291 -- Spring 2000
Case Statements CMP AX, BX JNE Case 2 <Do case 1> JMP Done Case 2: SUB AX, DX CMP AX, BX JNE Case 3 <Do case 2> JMP Done . . . . . . Done: <Continue> ECE 291 -- Spring 2000
Repeat/Until & While Looping -- LOOP instr. uses register CX to iterate -- CX is decremented and compared by the LOOP instr. -- Loop ends when CX=0 Example: MOV CX, Count loop_label: ... Loop Processing ... LOOP loop_label ECE 291 -- Spring 2000
LOOP Instruction -LOOP uses CX register for successive decrements. It jumps back to loop if CX is not a zero - When CX becomes 0 control transfers to instruction immediately following the loop Example: ADDS PROC MOV CX, 100 MOV SI, offset block1 MOV DI, offset block2 Loop_label: LODSW ; get block1 data: AX=[SI]; SI=SI+2 ADD AX, ES:[DI] ; add block2 data STOSW ; store in block2: [DI]=AX; DI=DI+2 LOOP Loop_label ; repeat 100 times RET ADDS ENDP ECE 291 -- Spring 2000
While Loop Example X86 Code: While_loop: MOV AX, J CMP AX, K JNGE Exit_loop DEC J INC K MOV AX, J IMUL AX, K MOV L, AX JMP While_loop Exit_loop: C-like Example: While (J>=K) do begin J:=J-1; K:=K+1; L=J*K; end; ECE 291 -- Spring 2000
OPCODE MODE Displacement Data Immediate value Instruction Format & Semantics Instruction: Instruction fields: - OPCODE (1/2 bytes) - Mode (1 byte) - Data (1/2 bytes) - Displacement (1/2 bytes) Descriptor bit (D): - D=0: 16-bit instructions - D=1: 32-bit instructions ECE 291 -- Spring 2000
Instruction Format & Semantics OPCODE: D - Direction of data transfers: - D = 0: data transfers from R/M field to Register - D = 0: data transfers TO the Register from R/M field in next byte W - Data size: - W=0: data size is a byte - W=1: data size is a word/double word | | | | | | D|W ECE 291 -- Spring 2000
MOD Format 00 No displacement 01 8-bit sign-extended disp. 10 16-bit displacement 11 R/M is a register MOD REG R/M 2 3 3 MOD: Selecting Addressing Mode - MOD field: 1 byte that specifies the addressing mode + enables/disables displacement. - Only when MOD=11 R/M is a register specifier ECE 291 -- Spring 2000
Code Function 000 DS: [BX+SI] 001 DS: [BX+DI] 010 SS: [BP+SI] 011 SS: [BP+DI] 100 DS: [SI] 101 DS: [DI] 110 SS: [BP] 111 DS: [BX] R/M Field: Selecting Addressing Mode -If MOD is 00, 01, 10, then R/M does not specify a register operand, but memory addressing mode: Examples: 1. If MOD=00 and R/M=101: addressing mode is [DI] 2. If MOD=01 and R/M=101 addressing mode is [DI +disp] 3. If MOD=10 and R/M=101 addressing mode is disp1[DI+disp2] ECE 291 -- Spring 2000