(a) circle representation of integers mod n
Post on 16-Jan-2016
37 Views
Preview:
DESCRIPTION
TRANSCRIPT
N 2-
N 1-0
1
2
(a) Circle representation of integers mod N
00000001
0010
0011
0100
0101
0110
01111000
1001
1010
1011
1100
1101
1110
1111
1+1-2+
3+
4+
5+
6+7+
2-
3-
4-
5-
6-7- 8-
0
(b) Mod 16 system for 2's-complement numbers
Figure 2.3. Modular number systems and the 2's-complement system.
1 0 1 11 1 1 0
1 0 0 1
1 1 0 11 0 0 1
0 0 1 00 1 0 0
0 1 1 00 0 1 1
1 0 0 11 0 1 1
1 0 0 10 0 0 1
0 0 1 01 1 0 1
1 1 1 0
0 1 0 01 0 1 0
0 1 1 11 1 0 1
0 1 0 0
1 1 0 10 1 1 1
0 1 0 0
0 0 1 01 1 0 0
1 1 1 0
0 1 1 01 1 0 1
0 0 1 1
1 0 0 10 1 0 1
1 1 1 0
1 0 0 11 1 1 1
1 0 0 0
0 0 1 00 0 1 1
0 1 0 1
0 1 0 1
0 0 1 00 0 1 1
5-
2+( )3+( )
5+( )
2+( )4+( )
2-
7-
3- 7-
6+( )3+( )
1+( )
7- 5-
7-
2+( )3-
6-
2-
4+( )
3-
4+( )
7+( )
4+( )
2-
3+( )
2-
8-
5+( )
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
(a)
(c)
(b)
(d)
(e)
(f)
(g)
(h)
(i)
(j)
Figure 2.4. 2's-complement Add and Subtract operations.
second word
first word
Figure 2.5. Memory words.
n bits
last word
i th word
•••
•••
R0,C
B,R0
A,R0
Movei + 8
Begin execution here Movei
ContentsAddress
C
B
A
the programData for
segmentprogram3-instruction
Addi + 4
Figure 2.8. A program for C +
NUM n
NUM2
NUM1
R0,SUM
NUM n,R0
NUM3,R0
NUM2,R0
NUM1,R0
Figure 2.9. A straight-line program for adding n numbers.
Add
Add
Move
SUM
i
Move
Add
i 4n+
i 4n 4-+
i 8+
i 4+
•••
•••
•••
N,R1Move
NUM n
NUM2
NUM1
R0,SUM
R1
"Next" number to R0
Figure 2.10. Using a loop to add n numbers.
LOOP
Decrement
Move
LOOP
loopProgram
Determine address of"Next" number and add
N
SUM
n
R0Clear
Branch>0
•••
•••
Operand1020
Figure 2.13. Indexed addressing.
Add 1000(R1),R2
R1
R1
Add 20(R1),R2
Operand1020
201000
20 = offset
20 = offset
10001000
(a) Offset is given as a constant
(b) Offset is in the index register
Figure 2.14. A list of students' marks.
Student 1
Student 2
Test 3
Test 2
Test 1
Student ID
Test 3
Test 2
Student ID
nN
LIST
Test 1LIST + 4
LIST + 8
LIST + 12
LIST + 16
•••
NUM2
NUMn
NUM1
R0Clear
R0,SUM
R1
#4,R2
(R2),R0
Figure 2.17. Memory arrangement for the program in Figure 2.12.
100
132
604
212
208
204
200
128
124
120
116
112
108
104
100
SUM
N
LOOP
LOOP
Decrement
Add
Add
Move
#NUM1,R2
N,R1Move
Move
Branch>0
Figure 2.21. A stack of words in the memory.
register
Stackpointer
17
BOTTOM
0
SP Currenttop element
elementBottom
Stack
2k
1-
•••
•••
•••
739
43
28-
Figure 2.22. Effect of stack operations on the stack in Figure 2.21.
(b) After pop into ITEM(a) After push from NEWITEM
17
739
43
ITEM
SP
Stack
SP
NEWITEM
19
17
739
19
43
28-
28-28-
•••
•••
ReturnCall
Figure 2.24. Subroutine linkage using a link register.
1000
204
204
Link
PC
Return
1000
locationMemory
Calling programMemorylocation
200204
Call SUBnext instruction
Subroutine SUB
first instruction
Assumetopofstack isatlevel1 below.
Move #NUM1, (SP) Pushparametersontostack.Move N, (SP)Call LISTADD Callsubroutine
(top ofstackat level2).Move 4(SP),SUM Saveresult.Add #8,SP Restoretopofstack
(top ofstackat level1)....
LISTADD MoveMultiple R0 R2, (SP) Saveregisters(top ofstackatlevel3).
Move 16(SP),R1 Initializecounter to n.Move 20(SP),R2 Initializepointer tothelist.Clear R0 Initializesumto 0.
LOOP Add (R2)+,R0 Add entryfromlist.Decrement R1Branch>0 LOOPMove R0,20(SP) Put resultonthestack.MoveMultiple (SP)+,R0 R2 Restoreregisters.Return Return tocallingprogram.
[R2]
[R1]
[R0]
Returnaddress
n
NUM1
Level3
Level2
Level1
––
– –
Figure 2.26. Program of Figure 2.16 written as a subroutine; parameters passed on the stack.
(a) Calling program and subroutine
(b) Top of stack at various times
–
Figure 2.27. A subroutine stack frame example.
SP(stack pointer)
FP(frame pointer)
saved [R1]
saved [R0]
Stackframefor
calledsubroutineReturn address
localvar3
localvar2
localvar1
saved [FP]
Old TOS
param2
param1
param3
param4
(top-of-stack)
Memorylocation Instructions Comments
Mainprogram...
2000 Move PARAM2, (SP) Placeparametersonstack.2004 Move PARAM1, (SP)2008 Call SUB12012 Move (SP),RESULT Storeresult.2016 Add #8,SP Restorestacklevel.2020 nextinstruction
...
Firstsubroutine
2100 SUB1 Move FP, (SP) Saveframepointerregister.2104 Move SP,FP Loadtheframepointer.2108 MoveMultiple R0 R3, (SP) Saveregisters.2112 Move 8(FP),R0 Get firstparameter.
Move 12(FP),R1 Getsecondparameter....Move PARAM3, (SP) Placeaparameteronstack.
2160 Call SUB22164 Move (SP)+,R2 PopSUB2result into R2.
...Move R3,8(FP) Placeansweronstack.MoveMultiple (SP)+,R0 R3 Restoreregisters.Move (SP)+,FP Restoreframepointerregister.Return Return toMainprogram.
Secondsubroutine
3000 SUB2 Move FP, (SP) Saveframepointerregister.Move SP ,FP Loadtheframepointer.MoveMultiple R0 R1, (SP) SaveregistersR0 andR1.Move 8(FP),R0 Get theparameter....Move R1,8(FP) PlaceSUB2resultonstack.MoveMultiple (SP)+,R0R1 RestoreregistersR0 andR1.Move (SP)+,FP Restoreframepointer register.Return ReturntoSubroutine1.
––
–
– –
–
–
–
– –
–
Figure 2.28. Nested subroutines.
Figure 2.29. Stack frames for Figure 2.28.
FP
FP
[FP] from SUB1
2164
Stackframe
forfirst
subroutine
[R0] from Main
param3
[R3] from Main
[R2] from Main
[R1] from Main
Old TOS
2012
[FP] from Main
param1
param2
[R0] from SUB1
[R1] from SUB1
Stackframefor
secondsubroutine
Figure 2.30. Logical and arithmetic shift instructions.
CR00
before:
after:
0
1
0 0 01 1 1 . . . 11
0 0 1 1 1 000
(b) Logical shift right LShiftR #2,R0
(a) Logical shift left LShiftL #2,R0
C R0 0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 10 . . . 00101
C
before:
after:
0
1
1 1 00 0 1 . . . 01
1 1 0 0 1 011
(c) Arithmetic shift right AShiftR #2,R0
R0
. . .
. . .
Figure 2.32. Rotate instructions.
CR0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 0 1 1 1 001
(c) Rotate right without carry RotateR #2,R0
(a) Rotate left without carry RotateL #2,R0
C R0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 10 . . . 10101
C
before:
after:
0
1
0 0 01 1 1 . . . 11
1 0 1 1 1 000
(d) Rotate right with carry RotateRC #2,R0
R0
. . .
. . .
(b) Rotate left with carry RotateLC #2,R0
C R0
before:
after:
0
1
0 0 01 1 1 . . . 11
1 10 . . . 00101
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;
}}
}
Move #LIST,R0 Load LIST intobaseregisterR0.Move N,R1 Initializeouterloopindex
Subtract #1,R1 registerR1to j = n 1.OUTER Move R1,R2 Initializeinner loopindex
Subtract #1,R1 registerR2to k= j 1.MoveByte (R0,R1),R3 LoadLIST( j) intoR3,whichholds
currentmaximuminsublist.INNER CompareByte R3,(R0,R2) If LIST(k) [R3],
Branch 0 NEXT donotexhange.MoveByte (R0,R2),R4 Otherwise,exchangeLIST(k)MoveByte R3,(R0,R2) with LIST(j )andloadMoveByte R4,(R0,R1) newmaximumintoR3.MoveByte R4,R3 RegisterR4servesas TEMP.
NEXT Decrement R2 Decrement indexregistersR2andBranch 0 INNER R1,which alsoserveDecrement R1 asloopcounters, andbranchBranch>0 OUTER back if loopsnotfinished.
(b) Assembly language program for sorting
Figure 2.34. A byte-sorting program using a straight-selection sort.
––
–
––
–
(a) C-language program for sorting
Figure 2.36. A list of student test scores organized as a linked list in memory.
First
28106 12001040
1 word 1 word 3 words
Head
(ID) (Test scores)
Memoryaddress
Keyfield
Linkfield
Datafield
record 27243 10402320
40632 12802720
28370 28801200
47871 01280
Secondrecord
Thirdrecord
Second last
Lastrecord
record
Tail
•••
INSERTION Compare
Branch>0Move RNEWREC, RHEAD
Return
Compare (RHEAD), (RNEWREC)
Branch>0 SEARCH
Move RHEAD, 4(RNEWREC)
Move RNEWREC, RHEAD
Return
Move RHEAD, RCURRENT
Move 4(RCURRENT), RNEXT
Compare
Branch=0 TAIL
(RNEXT), (RNEWREC)
Branch<0
#0, RNEXT
Compare
HEAD
not empty
#0, RHEAD
HEAD
INSERTMove RNEXT, RCURRENT
Move
Branch
RNEXT, 4(RNEWREC)Move RNEWREC, 4(RCURRENT)
Return
SEARCH
insert ne w recordsomewhere aftercurrent head
LOOP
insert ne w record inan interior position
new record becomes ne w tail
INSERT
TAIL
LOOP
ne w recordbecomes aone-entry list
ne w recordbecomes
ne w head
Figure 2.37. A subroutine for inserting a new record into a linked list.
OP code
Figure 2.39. Encoding instructions into 32-bit words.
Source Dest Other info
8 7 7 10
(b) Two-word instruction
Memory address/Immediate operand
OP code
(c) Three-operand instruction
R i R j Other infoR k
OP code Source Dest Other info
(a) One-word instruction
Table2.1Generic addressing modes
Name Assemblersyntax Addressingfunction
Immediate #Value Operand= Value
Register Ri EA= Ri
Absolute(Direct) LOC EA= LOC
Indirect (Ri ) EA= [Ri](LOC) EA= [LOC]
Index X(Ri) EA= [Ri]+ X
Basewithindex (Ri ,Rj ) EA= [Ri]+ [Rj]
Basewithindex X(Ri,Rj ) EA= [Ri]+ [Rj] + Xandoffset
Relative X(PC) EA= [PC] + X
Autoincrement (Ri )+ EA= [Ri] ;Increment R i
Autodecrement (Ri ) Decrement R i ;EA= [Ri]
EA =effectiveaddressValue = asigned number
top related