1 pertemuan 13 mesin ia-32 pentium: i matakuliah: t0324 / arsitektur dan organisasi komputer tahun:...
TRANSCRIPT
1
Pertemuan 13Mesin IA-32 Pentium: I
Matakuliah : T0324 / Arsitektur dan Organisasi Komputer
Tahun : 2005
Versi : 1
2
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Membandingkan implementasi instruksi mesin untuk Arsitektur Komputer Intel Pentium ( C4 ) ( No TIK : 6 )
3
Chapter 3.
The IA-32 PentiumInstruction Set: I
(OFC3)
4
Figure 3.37. IA-32 register structure.
31 13 11 9 7 0
Instruction pointer
CF - CarryZF - ZeroSF - SignTF - Trap
IOPL - Input/Output
OF - Overflow
IF - Interrupt enable
R0
R1
31 0
R7
FP0
FP1
FP7
63 0
CS
16 0
SS
ES
FS
GS
DS
Code Segment
Stack Segment
Data Segments
31 0
Status register
12 8 6
privilege level
Generalpurposeregisters
8
Floating-pointregisters
8
Segmentregisters
6
5
31 15 7 0816
registersData
R0
R1
R2
R3
R4
R5
R6
R7
Figure 3.38. Compatibility of the IA-32 register structure
with earlier Intel processor register structures.
AH AL
AX
CH CL
CX
DH DL
DX
BH BL
BX
Generalpurposenaming
EAX
ECX
EDX
EBX
ESP
EBP
ESI
EDI
EIP
EFLAGS
SP
SI
BP
DI
IP
FLAGS
registersPointer
registersIndex
pointerInstruction
registerStatus
Figure 3.38. Compatibility of the IA-32 register structure with earlier Intelprocessor register structures.
6
1000
60 = displacement
1000
doubleword
1060 Operand
Figure 3.39. Examples of addressing modes in the IA-32 architecture.
Mainmemoryaddress
Base register EBP
Operand address (EA) = [EBP] + 60
(a) Base with displacement mode, expressed as [EBP + 60]
1000
200 = displacement
1000
1200
Operand
Base register EBP
Operand address (EA) = [EBP] + [ESI] 4 + 200
(b) Base with displacement and index mode, expressed as [EBP + ESI * 4 + 200]
1360
List of 4-byte (double w ord) data items
40
Index register ESI
scale factor = 4
160 = [Index register] 4
***
***
***
***
***
***
***
***
***
***
7
LEA EBX,NUM1 Initialize base (EBX) andMO V ECX,N counter (ECX) registers.MO V EAX,0 Clear accumulator (EAX)MO V EDI,0 and index (EDI) registers.
ST AR TADD: ADD EAX,[EBX + EDI 4] Add next number in to EAX.INC EDI Incremen t index register.DEC ECX Decremen t coun ter register.JG ST AR TADD Branc h back if [ECX] > 0.MO V SUM,EAX Store sum in memory .
(a) Straightforward approach
LEA EBX,NUM1 Load base register EBX andSUB EBX,4 adjust to hold NUM1 4.MO V ECX,N Initialize counter/index (ECX).MO V EAX,0 Clear the accumulator (EAX).
ST AR TADD: ADD EAX,[EBX + ECX 4] Add next number in to EAX.LOOP ST AR TADD Decremen t ECX and branc h
back if [ECX] > 0.MO V SUM,EAX Store sum in memory .
(b) More compact program
Figure 3.40. IA-32 program for adding numbers.
*
*
–
8
Figure 3.41. IA-32 instruction format.
ImmediateDisplacementAddressing
OP code mode
1 or 2bytes
1 or 2bytes
1 or 4bytes
1 or 4bytes
9
Assemblerdirectives
.dataNUM1 DD 17, 3, 51,242, 113N DD 5SUM DD 0
.code
Statements thatgeneratemachineinstructions
MAIN : LEA EBX ,NUM1SUB EBX ,4MOV ECX ,NMOV EAX , 0
STARTADD : ADD EAX , [EBX+ECX 4]LOOP STARTADDMOV SUM,EAX
Assemblerdirective END MAIN
Figure 3.42. Complete IA-32 assembly language representation for the program in Figure 3.40b.
*
10
LEA EBP,LOC EBP points tofirst byte.MOV AL,[EBP] Loadfirst byteintoAL.SHL AL,4 Shiftleft by 4bit positions.MOV BL,[EBP+1] Loadsecondbyteinto BL.AND BL,0FH Clearhigh-order4bitsto zero.OR AL,BL ConcatenatetheBCDdigits.MOV PACKED,AL Storetheresult.
Figure 3.43. An IA-32 routine to pack two BCD digits into a byte.
11
LEA EBP,LOC EBP points tomemoryarea.READ: BT INSTATUS,3 Wait forcharacterto be
JNC READ enteredinto DATAIN.MOV AL,DATAIN Transfercharacterinto AL.MOV [EBP],AL StorethecharacterinmemoryINC EBP andincrementpointer.
ECHO: BT OUTSTATUS,3 Wait fordisplay toJNC ECHO beready.MOV DATAOUT,AL Sendcharactertodisplay.CMP AL,CR If notcarriagereturn,JNE READ readmorecharacters.
Figure 3.44. An IA-32 program that reads a line of characters and displays it.
12
Pertemuan 14Mesin IA-32 Pentium: II
Matakuliah : T0324 / Arsitektur dan Organisasi Komputer
Tahun : 2005
Versi : 1
13
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Membandingkan implementasi instruksi mesin untuk Arsitektur Komputer Intel Pentium ( C4 ) ( No TIK : 6 )
14
Chapter 3.
The IA-32 PentiumInstruction Set: II
(OFC4)
15
Calling program...LEA EBX,NUM1 Load parametersMO V ECX,N into EBX,ECX.CALL LIST ADD Branc h to subroutine.MO V SUM,EAX Store sum into memory ....
SubroutineLIST ADD: PUSH EDI Save EDI.
MO V EDI,0 Use EDI as index register.MO V EAX,0 Use EAX as accumulator register.
ST AR TADD: ADD EAX, [EBX + EDI 4] Add next number.INC EDI Incremen t index.DEC ECX Decremen t counter.JG STARTADD Branc h back if [ECX] > 0.POP EDI Restore EDI.RET Branc h bac k to Calling program.
(a) Calling program and subroutine
ESP [EDI]Return Address
Old TOS
(b) Stack contents after saving EDI in subroutine
Figure 3.45. Program of Figure 3.40a written as an IA-32 subroutine;parameters passed through registers.
*
16
(Assume top of stac k is at level 1 below.)Calling program
PUSH OFFSET NUM1 Push parameters onto the stack.PUSH NCALL LIST ADD Branc h to the subroutine.ADD ESP ,4 Remo ve n from the stack.POP SUM Pop the sum into SUM....
SubroutineLIST ADD: PUSH EDI Save EDI and use
MO V EDI,0 as index register.PUSH EAX Save EAX and use asMO V EAX,0 accummulator register.PUSH EBX Save EBX and loadMO V EBX,[ESP+20] address NUM1.PUSH ECX Save ECX andMO V ECX,[ESP+20] load count n .
ST AR TADD: ADD EAX,[EBX+EDI 4] Add next number.INC EDI Incremen t index.DEC ECX Decremen t coun ter.JG ST AR T ADD Branc h bac k if not done.MO V [ESP+24],EAX Overwrite NUM1 in stac k with sum.POP ECX Restore registers.POP EBXPOP EAXPOP EDIRET Return.
(a) Calling program and subroutine
[ECX][EBX][EAX][EDI]
Return Addressn
NUM1
Lev el 3
Lev el 2
Lev el 1
(b) Stack contents at different times
Figure 3.46. Program of Figure 3.40a written as an IA-32 subroutine; parameters passed on the stack.
*
17
Address Instructions Commen ts
Calling program ...2000 PUSH PARAM2 Place parameters2006 PUSH PARAM1 on stack.2012 CALL SUB12017 POP RESUL T Store result.
ADD ESP ,4 Restore stack level....First subroutine2100 SUB1: PUSH EBP Save frame pointer register.
MO V EBP ,ESP Load frame pointer.PUSH EAX Saveregisters.PUSH EBXPUSH ECXPUSH ED XMO V EAX,[EBP+8] Get first parameter.MO V EBX,[EBP+12] Get second parameter....PUSH PARAM3 Place parameter on stack.
2160 CALL SUB22165 POP ECX Pop SUB2 result into ECX....
MO V [EBP+8],ED X Place answ er on stack.POP ED X Restore registers.POP ECXPOP EBXPOP EAXPOP EBP Restore frame pointer register.RET Return to Main program.
Second subroutine3000 SUB2: PUSH EBP Save frame pointer register.
MO V EBP ,ESP Load frame pointer.PUSH EAX Saveregisters.PUSH EBXMO V EAX,[EBP+8] Get parameter....MO V [EBP+8],EBX Place SUB2 result on stack.POP EBX Restore registers.POP EAXPOP EBP Restore frame pointer register.RET Return to first subroutine.
Figure 3.47. Nested subroutines in IA-32 assembly language.
18
Figure 3.48. IA-32 stack frames for Figure 3.47.
EBP
EBP
[EBP] from SUB1
2165
Stackframe
forfirst
subroutine
[EAX] from Main
param3
[EDX] from Main
[ECX] from Main
[EBX] from Main
Old TOS
2017
[EBP] from Main
param1
param2
[EAX] from SUB1
[EBX] from SUB1
Stackframe
forsecond
subroutine
19
LEA EBP,AVEC EBP points to vectorA.LEA EBX,BVEC EBX pointsto vectorB.MOV ECX,N ECX isthe loopcounter.MOV EAX,0 EAX accumulatesthedotproduct.MOV EDI,0 EDI isanindexregister.
LOOPSTART: MOV EDX,[EBP+EDI 4] ComputetheproductIMUL EDX,[EBX+EDI 4] ofnextcomponents.INC EDI Increment index.ADD EAX,EDX Addtoprevioussum.LOOP LOOPSTART Branch back if notdone.MOV DOTPROD,EAX Storedotproductinmemory.
Figure 3.49. IA-32 dot product program.
**
20
for (j = n 1; j > 0; j = j 1){ for ( k = j 1; k >= 0; k = k 1 )
{ if (LIST[ k ] > LIST[ j ]){ TEMP = LIST[ k ];
LIST[ k] = LIST[ j ];LIST[ j ] = TEMP;
}}
}
(a) C-language program for sorting
LEA EAX,LIST Load list poin ter baseMO V EDI,N register (EAX), and initializeDEC EDI outer loop index register
(EDI) to j = n 1.OUTER: MO V ECX,EDI Initialize inner loop index
DEC ECX register (ECX) to k = j 1.MO V DL,[EAX + EDI] Load LIST(j) into register DL.
INNER: CMP [EAX + ECX],DL Compare LIST(k) to LIST(j).JLE NEXT If LIST(k) LIST(j), go to
next lower k index entry;X CHG [EAX + ECX],DL Otherwise, interchange LIST(k)
and LIST(j), leavingMO V [EAX + EDI],DL new LIST(j) in DL.
NEXT: DEC ECX Decremen t inner loop index k.JGE INNER Repeat or terminate inner loop.DEC EDI Decremen t outer loop index j.JG OUTER Repeat or terminate outer loop.
(b) IA-32 program implementation
Figure 3.50. An IA-32 byte-sorting program using straight-selection sort.
– –––
–
–
21
Subroutine
INSERTION: MOV RNEWID,[RNEWREC]CMP RHEAD,0 Check if list empty.JG HEADMOV RHEAD,RNEWREC If yes,newrecordbecomesRET one-entry list.
HEAD: CMP RNEWID,[RHEAD] Check if newrecordbecomeshead.
JG SEARCHMOV [RNEWREC+4],RHEAD If yes,make newrecordMOV RHEAD,RNEWREC thehead.RET
SEARCH: MOV RCURRENT,RHEAD Otherwise,useLOOPSTART: MOV RNEXT,[RCURRENT+4] RCURRENT
CMP RNEXT,0 andRNEXTJE TAIL to movethroughCMP RNEWID,[RNEXT] thelist tofindJL INSERT theinsertionpoint.MOV RCURRENT,RNEXTJMP LOOPSTART
INSERT: MOV [RNEWREC+4],RNEXTTAIL: MOV [RCURRENT+4],RNEWREC
RET
Figure 3.51. An IA-32 subroutine for inserting a new record into a linked list.
22
Subroutine
DELETION: CMP RIDNUM,[RHEAD] Check if head.JGT SEARCHMOV RHEAD,[RHEAD+4] If yes,remove.RET
SEARCH: MOV RCURRENT,RHEAD Otherwise,LOOPSTART: MOV RNEXT,[RCURRENT+4] useRCURRENT
CMP RIDNUM,[RNEXT] and RNEXTJEQ DELETE to movethroughMOV RCURRENT,RNEXT thelist toJMP LOOPSTART findtherecord.
DELETE: MOV RTEMP,[RNEXT+4]MOV [RCURRENT+4],RTEMPRET
Figure 3.52. An IA-32 subroutine for deleting a record from a linked list.
23
Program1 Program2
CLR.L D0 MOVE.W #$FFFF,D0MOVEA.L #LIST,A0 MOVEA.L #LIST,A0
LOOP MOVE.W (A0)+,D1 LOOP LSL.W (A0)+BGE LOOP BCC LOOPADDQ.L #1,D0 LSL.W #1,D0CMPI #17,D0 BCS LOOPBLT LOOP MOVE.W 2(A0),RSLTMOVE.W 2(A0),RSLT
Figure P3.1. Two 68000 programs for Problem 3.29.
––
24
Table 3.3IA-32 addressing modes
Name Assembler syntax Addressing function
Immediate V alue Op erand = ValueDirect Lo cation EA = Lo cation
Register Reg EA = Regthat is, Op erand = [Reg]
Register indirect [Reg] EA = [Reg]Base with [Reg + Disp] EA = [Reg] + Disp
displacemen t
Index with [Reg S + Disp] EA = [Reg] S + Dispdisplacemen t
Base with index [Reg1 + Reg2 S] EA = [Reg1] + [Reg2] S
Base with index [Reg1 + Reg2 S + Disp] EA = [Reg1] + [Reg2] S + Dispand displacemen t
Value = an 8- or 32-bit signed numberLocation = a 32-bit addressReg, Reg1, Reg2 = one of the general purpose registers EAX, EBX, ECX,
ED X, ESP , EBP , ESI, EDI, with the exception thatESP cannot be used as an index register
Disp = an 8- or 32-bit signed number, except that in the Index withdisplacement mode it can only be 32 bits.
S = a scale factor of 1, 2, 4, or 8
*
*
*