Matakuliah
Tahun
Versi
: T0324 / Arsitektur dan Organisasi Komputer
: 2005
:1
Pertemuan 13
Mesin IA-32 Pentium: I
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Membandingkan implementasi instruksi
mesin untuk Arsitektur Komputer Intel
Pentium ( C4 ) ( No TIK : 6 )
2
Chapter 3.
The IA-32 Pentium
Instruction Set: I
(OFC3)
3
31
0
R0
R1
8
General
purpose
registers
R7
63
0
FP0
FP1
8
Floating-point
registers
FP7
16
Code Segment
Stack Segment
0
CS
SS
6
Segment
registers
DS
Data Segments
ES
FS
GS
31
0
Instruction pointer
31
13 12 11
9 8 7
6
0
Status register
IOPL - Input/Output
priv ilege lev el
OF - Ov erf low
CF - Carry
ZF - Zero
SF - Sign
TF - Trap
IF - Interrupt enable
Fi gure 3.37.
IA-32 regi ster structure.
4
DX
R3
EBX
BH
BL
BX
R4
ESP
SP
R5
EBP
BP
R6
ESI
SI
R7
EDI
EIP
EFLAGS
DI
IP
FLAGS
Pointer
registers
Index
registers
Instruction
pointer
Status
register
Fi gure 3.38. Com pati bi l i ty of the IA-32 regi ster structure
wi th earl i er Intel processor regi ster structures.
Figure 3.38. Compatibility of the IA-32 register structure with earlier Intel
processor register structures.
5
Main
memory
address
doubleword
1000
Base register EBP
1000
*
*
*
1060
*
*
*
60 = displacement
Operand
Operand address (EA) = [EBP] + 60
(a) Base with displacement mode, expressed as [EBP + 60]
1000
Base register EBP
1000
40
*
*
*
*
*
*
Index register ESI
200 = displacement
1200
List of 4-byte
(double w ord)
data items
*
*
*
**
**
**
1360
scale factor = 4
*
*
*
160 = [Index register]
4
Operand
Operand address (EA) = [EBP] + [ESI]
4 + 200
(b) Base with displacement and index mode, expressed as [EBP + ESI * 4 + 200]
Figure 3.39.
Examples of addressing modes in the IA-32 architecture.
6
ST AR T ADD:
LEA
MO V
MO V
MO V
ADD
INC
DEC
JG
MO V
EBX,NUM1
ECX,N
EAX,0
EDI,0
EAX,[EBX
+ EDI * 4]
EDI
ECX
ST AR T ADD
SUM,EAX
Initialize
base (EBX)
and
coun ter (ECX) registers.
Clear accum ulator (EAX)
and index (EDI)
registers.
Add next n um b er in to EAX.
Incremen t index register.
Decremen t coun ter register.
Branc h bac k if [ECX] > 0.
Store sum in memory .
(a) Straightforward approach
ST AR T ADD:
LEA
SUB
MO V
MO V
ADD
LOOP
EBX,NUM1
EBX,4
ECX,N
EAX,0
EAX,[EBX
+ ECX * 4]
ST AR T ADD
MO V
SUM,EAX
Load base register EBX and
adjust to hold NUM1 – 4.
Initialize
coun ter/index
(ECX).
Clear the accum ulator
(EAX).
Add next num ber in to EAX.
Decremen t ECX and branc h
bac k if [ECX] > 0.
Store sum in memory .
(b) More compact program
Figure 3.40. IA-32 program for adding numbers.
7
OP code
Addressing
mode
Displacement
Immediate
1 or 2
by tes
1 or 2
by tes
1 or 4
by tes
1 or 4
by tes
Figure 3.41. IA-32 instruction format.
8
Assembler directives
.data
NUM1
N
SUM
DD
DD
DD
17, 3,51, 242,113
5
0
LEA
SUB
MOV
MOV
ADD
LOOP
MOV
EBX , NUM1
EBX , 4
ECX , N
EAX , 0
EAX , [EBX +ECX * 4]
STARTADD
SUM , EAX
END
MAIN
.code
MAIN :
Statemen
ts that generate
machine instructions
Assembler directive
STARTADD :
Figure 3.42. Complete IA-32 assembly language representation for the program in Figure 3.40b.
9
LEA
MOV
SHL
MOV
AND
OR
MOV
EBP,LOC
AL,[EBP]
AL,4
BL,[EBP+1]
BL,0FH
AL,BL
PACKED,AL
EBP points to first byte.
Load first byte into AL.
Shift left by 4 bit positions.
Load secondbyte into BL.
Clear high-order 4 bits to zero.
Concatenatethe BCD digits.
Store the result.
Figure 3.43. An IA-32 routine to pack two BCD digits into a byte.
10
READ:
ECHO:
LEA
BT
JNC
MOV
MOV
INC
BT
JNC
MOV
CMP
JNE
EBP,LOC
INSTATUS,3
READ
AL,DATAIN
[EBP],AL
EBP
OUTSTATUS,3
ECHO
DATAOUT,AL
AL,CR
READ
EBP points to memory area.
Wait for characterto be
entered into DATAIN.
Transfer characterinto AL.
Store the characterin memory
and increment pointer.
Wait for display to
be ready.
Sendcharacterto display.
If not carriagereturn,
read more characters.
Figure 3.44. An IA-32 program that reads a line of characters and displays it.
11
Matakuliah
Tahun
Versi
: T0324 / Arsitektur dan Organisasi Komputer
: 2005
:1
Pertemuan 14
Mesin IA-32 Pentium: II
12
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Membandingkan implementasi instruksi
mesin untuk Arsitektur Komputer Intel
Pentium ( C4 ) ( No TIK : 6 )
13
Chapter 3.
The IA-32 Pentium
Instruction Set: II
(OFC4)
14
Calling
program
Subroutine
LIST ADD:
ST AR T ADD:
..
.
LEA
MO V
CALL
MO V
..
.
EBX,NUM1
ECX,N
LIST ADD
SUM,EAX
Load parameters
in to EBX,ECX.
Branc h to subroutine.
Store sum in to memory .
PUSH
MO V
MO V
ADD
INC
DEC
JG
POP
RET
EDI
EDI,0
EAX,0
EAX, [EBX + EDI * 4]
EDI
ECX
STARTADD
EDI
Sa v e EDI.
Use EDI as index register.
Use EAX as accum ulator register.
Add next num b er.
Incremen t index.
Decremen t coun ter.
Branc h bac k if [ECX] > 0.
Restore EDI.
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.
15
(Assume top of stac k is at lev el 1 belo w.)
Calling program
PUSH
OFFSET
NUM1
PUSH
N
CALL
LIST ADD
ADD
ESP ,4
POP
SUM
..
.
Subroutine
LIST ADD:
PUSH
EDI
MO V
EDI,0
PUSH
EAX
MO V
EAX,0
PUSH
EBX
MO V
EBX,[ESP+20]
PUSH
ECX
MO V
ECX,[ESP+20]
ST AR T ADD:
ADD
EAX,[EBX+EDI
* 4]
INC
EDI
DEC
ECX
JG
ST AR T ADD
MO V
[ESP+24],EAX
POP
ECX
POP
EBX
POP
EAX
POP
EDI
RET
Push parameters
on to the stack.
Branc h to the subroutine.
Remo ve n from the stack.
P op the sum in to SUM.
Sav e EDI and use
as index register.
Sav e EAX and use as
accummulator register.
Sav e EBX and load
address NUM1.
Sav e ECX and
load count n .
Add next number.
Incremen t index.
Decremen t coun ter.
Branc h bac k if not done.
Ov erwrite NUM1 in stac k with
Restore registers.
sum.
Return.
(a) Calling program and subroutine
Lev el 3
Lev el 2
Lev el 1
[ECX]
[EBX]
[EAX]
[EDI]
Return Address
n
NUM1
(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.
16
Address
Instructions
program .
..
2000
PUSH
2006
PUSH
2012
CALL
2017
POP
ADD
..
.
First subroutine
2100
SUB1:
PUSH
MO V
PUSH
PUSH
PUSH
PUSH
MO V
MO V
..
.
PUSH
2160
CALL
2165
POP
..
.
MO V
POP
POP
POP
POP
POP
RET
Second subroutine
3000
SUB2:
PUSH
MO V
PUSH
PUSH
MO V
..
.
MO V
POP
POP
POP
RET
Commen ts
Calling
P ARAM2
P ARAM1
SUB1
RESUL T
ESP ,4
Place parameters
on stac k.
EBP
EBP ,ESP
EAX
EBX
ECX
ED X
EAX,[EBP+8]
EBX,[EBP+12]
Sav e frame poin ter register.
Load frame poin ter.
Sav e registers.
P ARAM3
SUB2
ECX
Place parameter
[EBP+8],ED
ED X
ECX
EBX
EAX
EBP
Store result.
Restore stac k lev el.
Get first parameter.
Get second parameter.
P op SUB2
X
on stac k.
result in to ECX.
Place answ er on stac k.
Restore registers.
Restore frame p oin ter register.
Return to Main program.
EBP
EBP ,ESP
EAX
EBX
EAX,[EBP+8]
Sav e frame poin ter register.
Load frame p oin ter.
Sav e registers.
[EBP+8],EBX
EBX
EAX
EBP
Place SUB2 result on stac k.
Restore registers.
Get parameter.
Restore frame poin ter register.
Return to first subroutine.
Figure 3.47. Nested subroutines in IA-32 assembly language.
17
18
LOOPSTART:
LEA
LEA
MOV
MOV
MOV
MOV
IMUL
INC
ADD
LOOP
MOV
EBP,AVEC
EBX,BVEC
ECX,N
EAX,0
EDI,0
EDX,[EBP+EDI* 4]
EDX,[EBX+EDI * 4]
EDI
EAX,EDX
LOOPSTART
DOTPROD,EAX
EBP points to vectorA.
EBX points to vector B.
ECX is the loop counter.
EAX accumulatesthe dot product.
EDI is an index register.
Computethe product
of next components.
Increment index.
Add to previous sum.
Branch back if not done.
Store dot product in memory.
Figure 3.49. IA-32 dot product program.
19
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
OUTER:
INNER:
NEXT:
LEA
MO V
DEC
EAX,LIST
EDI,N
EDI
MO V
DEC
MO V
CMP
JLE
ECX,EDI
ECX
DL,[EAX
+ EDI]
[EAX + ECX],DL
NEXT
X CHG
[EAX
MO V
DEC
JGE
DEC
JG
[EAX + EDI],DL
ECX
INNER
EDI
OUTER
+ ECX],DL
Load list poin ter base
register (EAX),
and initialize
outer lo op index register
(EDI) to j = n – 1.
Initialize
inner lo op index
register (ECX) to k = j – 1.
Load LIST(j)
in to register DL.
Compare LIST(k)
to LIST(j).
If LIST(k) LIST(j),
go to
next lo w er k index en try;
Otherwise,
in terc hange LIST(k)
and LIST(j),
lea ving
new LIST(j)
in DL.
Decremen t inner lo op index k.
Rep eat or terminate
inner lo op.
Decremen t outer lo op index j.
Rep eat or terminate outer lo op.
(b) IA-32 program implementation
Figure 3.50. An IA-32 byte-sorting program using straight-selection sort.
20
Subroutine
INSERTION:
HEAD:
SEARCH:
LOOPSTART:
INSERT:
TAIL:
MOV
CMP
JG
MOV
RET
CMP
RNEWID,[RNEWREC]
RHEAD,0
HEAD
RHEAD,RNEWREC
JG
MOV
MOV
RET
MOV
MOV
CMP
JE
CMP
JL
MOV
JMP
MOV
MOV
RET
SEARCH
[RNEWREC+4],RHEAD
RHEAD,RNEWREC
RNEWID,[RHEAD]
RCURRENT,RHEAD
RNEXT,[RCURRENT+4]
RNEXT,0
TAIL
RNEWID,[RNEXT]
INSERT
RCURRENT,RNEXT
LOOPSTART
[RNEWREC+4],RNEXT
[RCURRENT+4],RNEWREC
Check if list empty .
If yes, new record becomes
one-entry list.
Check if new record
becomeshead.
If yes, make new record
the head.
Otherwise,use
RCURRENT
and RNEXT
to move through
the list to find
the insertionpoint.
Figure 3.51. An IA-32 subroutine for inserting a new record into a linked list.
21
Subroutine
DELETION:
SEARCH:
LOOPSTART:
DELETE:
CMP
JGT
MOV
RET
MOV
MOV
CMP
JEQ
MOV
JMP
MOV
MOV
RET
RIDNUM,[RHEAD]
SEARCH
RHEAD,[RHEAD+4]
Check if head.
RCURRENT,RHEAD
RNEXT,[RCURRENT+4]
RIDNUM,[RNEXT]
DELETE
RCURRENT,RNEXT
LOOPSTART
RTEMP ,[RNEXT+4]
[RCURRENT+4],R
TEMP
Otherwise,
useRCURRENT
and RNEXT
to move through
the list to
find the record.
If yes, remove.
Figure 3.52. An IA-32 subroutine for deleting a record from a linked list.
22
Program 1
LOOP
CLR.L
MOVEA.L
MOVE.W
BGE
ADDQ.L
CMPI
BLT
MOVE.W
D0
#LIST,A0
(A0)+,D1
LOOP
#1,D0
#17,D0
LOOP
– 2(A0),RSLT
Program 2
LOOP
MOVE.W
MOVEA.L
LSL.W
BCC
LSL.W
BCS
MOVE.W
#$FFFF,D0
#LIST,A0
(A0)+
LOOP
#1,D0
LOOP
– 2(A0),RSLT
Figure P3.1. Two 68000 programs for Problem 3.29.
23
T able 3.3
IA-32 addressing modes
Name
Assem bler
Immediate
Direct
V alue
Lo cation
Op erand = V alue
EA = Lo cation
Register
Reg
EA = Reg
that is, Op erand
Register
indirect
Base with
displacemen t
[Reg]
[Reg + Disp]
EA =
EA =
[Reg]
[Reg] + Disp
Index with
displacemen t
Base with index
[Reg * S + Disp]
EA =
[Reg] S + Disp
[Reg1 + Reg2 * S]
[Reg1 + Reg2 * S + Disp]
EA =
[Reg1]
+ [Reg2] S
EA =
[Reg1]
+ [Reg2] S + Disp
Base with index
and displacemen
t
V alue
Lo cation
Reg, Reg1, Reg2
=
=
=
Disp
=
S
=
syn tax
Addressing
function
= [Reg]
an 8- or 32-bit signed n um b er
a 32-bit address
one of the general purp ose registers
EAX, EBX, ECX,
ED X, ESP , EBP , ESI, EDI, with the exception
that
ESP cannot be used as an index register
an 8- or 32-bit signed n um ber, except that in the Index
displacemen t mode it can only be 32 bits.
a scale factor of 1, 2, 4, or 8
with
24
© Copyright 2026 Paperzz