230 likes | 325 Views
0. 1. N. -. 1. N. -. 2. 2. (a) Circle representation of integers mod N. 0000. 1111. 0001. 1110. 0010. 0. -. 1. +. 1. -. 2. +. 2. 1101. 0011. -. 3. +. 3. -. 4. +. 4. 1100. 0100. -. 5. +. 5. 1011. 0101. -. 6. +. 6. -. 7. +. 7. -. 8. 1010. 0110.
E N D
0 1 N - 1 N - 2 2 (a) Circle representation of integers modN 0000 1111 0001 1110 0010 0 - 1 + 1 - 2 + 2 1101 0011 - 3 + 3 - 4 + 4 1100 0100 - 5 + 5 1011 0101 - 6 + 6 - 7 + 7 - 8 1010 0110 1001 0111 1000 (b) Mod 16 system for 2's-complement numbers Figure 2.3. Modular number systems and the 2's-complement system.
( + 4 ) 0 0 1 0 ( + 2 ) 0 1 0 0 ( ) 0 0 1 1 + 3 1 0 1 0 ( - 6 ) ( - 2 ) 0 1 0 1 ( + 5 ) 1 1 1 0 ( ) + 7 1 0 1 1 ( - 5 ) 0 1 1 1 1 1 1 0 ( - 2 ) 1 1 0 1 ( - 3 ) 1 0 0 1 ( - 7 ) 0 1 0 0 ( + 4 ) 1 1 0 1 1 1 0 1 ( - 3 ) 1 0 0 1 0 1 1 1 ( - 7 ) 0 1 0 0 ( + 4 ) 0 0 1 0 ( + 2 ) 0 0 1 0 ( ) 0 1 0 0 + 4 1 1 0 0 1 1 1 0 ( - 2 ) 0 1 1 0 0 1 1 0 ( + 6 ) ( + 3 ) 0 0 1 1 1 1 0 1 0 0 1 1 ( + 3 ) 1 0 0 1 1 0 0 1 ( - 7 ) 0 1 0 1 1 0 1 1 ( - 5 ) 1 1 1 0 ( - 2 ) 1 0 0 1 ( - 7 ) 1 0 0 1 0 0 0 1 ( + 1 ) 1 1 1 1 1 0 0 0 ( - 8 ) 0 0 1 0 0 0 1 0 ( + 2 ) 1 1 0 1 0 0 1 1 ( - 3 ) ( ) 0 1 0 1 + 5 (a) (b) + + (c) (d) + + (e) + - (f) - + (g) - + (h) - + (i) - + (j) - + Figure 2.4.2's-complement Add and Subtract operations.
n bits first word second word • • • i th word • • • last word Figure 2.5. Memory words.
Address Contents i Begin execution here Move A,R0 3-instruction i + 4 program Add B,R0 segment i + 8 Move R0,C A Data for B the program C Figure 2.8. A program for C ¬ [A] + [B].
Move NUM1,R0 i i + 4 Add NUM2,R0 i + 8 Add NUM3,R0 • • • i Add NUM n ,R0 + 4 n - 4 i 4 n + Move R0,SUM • • • SUM NUM1 NUM2 • • • NUM n Figure 2.9. A straight-line program for addingn numbers.
Move N,R1 Clear R0 LOOP Determine address of "Next" number and add "Next" number to R0 Program loop Decrement R1 Branch>0 LOOP Move R0,SUM • • • SUM N n NUM1 NUM2 • • • NUM n Figure 2.10. Using a loop to addn numbers.
Add 20(R1),R2 R1 1000 1000 20 = offset 1020 Operand (a) Offset is given as a constant Add 1000(R1),R2 1000 20 R1 20 = offset 1020 Operand (b) Offset is in the index register Figure 2.13. Indexed addressing.
n N Student ID LIST LIST + 4 Test 1 Student 1 LIST + 8 Test 2 LIST + 12 Test 3 LIST + 16 Student ID Test 1 Student 2 Test 2 Test 3 • • • Figure 2.14. A list of students' marks.
100 Move N,R1 104 Move #NUM1,R2 108 Clear R0 LOOP 112 Add (R2),R0 116 Add #4,R2 120 Decrement R1 124 Branch>0 LOOP 128 Move R0,SUM 132 SUM 200 N 204 100 NUM1 208 NUM2 212 NUM n 604 Figure 2.17. Memory arrangement for the program in Figure 2.12.
0 • • • Stack pointer register Current SP - 28 top element 17 739 Stack • • • Bottom BOTTOM 43 element • • • k 2 - 1 Figure 2.21. A stack of words in the memory.
SP 19 - 28 - 28 17 SP 17 739 739 Stack • • • • • • 43 43 NEWITEM 19 ITEM - 28 (a) After push from NEWITEM (b) After pop into ITEM Figure 2.22. Effect of stack operations on the stack in Figure 2.21.
Memory Memory location Calling program location Subroutine SUB 200 Call SUB 1000 first instruction 204 next instruction Return 1000 PC 204 Link 204 Call Return Figure 2.24. Subroutine linkage using a link register.
Assume top of stac k is at lev el 1 b elo w. – Mo v e #NUM1, (SP) Push parameters on to stac k. – Mo v e N, (SP) Call LIST ADD Call subroutine (top of stac k at lev el 2). Mo v e 4(SP),SUM Sa v e result. Add #8,SP Restore top of stac k (top of stac k at lev el 1). . . . – – LIST ADD Mo v eMultiple R0 R2, (SP) Sa v e registers (top of stac k at lev el 3). Mo v e 16(SP),R1 Initialize coun ter to n . Mo v e 20(SP),R2 Initialize p oin ter to the list. Clear R0 Initialize sum to 0. LOOP Add (R2)+,R0 Add en try from list. Decremen t R1 Branc h LOOP > 0 Mo v e R0,20(SP) Put result on the stac k. – Mo v eMultiple (SP)+,R0 R2 Restore registers. Return Return to calling program. (a) Calling program and subroutine [R2] Lev el 3 [R1] [R0] Return address Lev el 2 n NUM1 Lev el 1 (b) Top of stack at various times Figure 2.26. Program of Figure 2.16 written as a subroutine; parameters passed on the stack.
SP saved [R1] (stack pointer) saved [R0] localvar3 localvar2 localvar1 Stack frame FP for saved [FP] (frame pointer) called subroutine Return address param1 param2 param3 param4 Old TOS (top-of-stack) Figure 2.27. A subroutine stack frame example.
Memory lo cation Instructions Commen ts Main program . . . – 2000 Mo v e P ARAM2, (SP) Place parameters on stack. – 2004 Mo v e P ARAM1, (SP) 2008 Call SUB1 2012 Mo v e (SP),RESUL T Store result. 2016 Add #8,SP Restore stack lev el. 2020 next instruction . . . First subroutine – 2100 SUB1 Mo v e FP , (SP) Sa v e frame p oin ter register. 2104 Mo v e SP ,FP Load the frame p oin ter. – – 2108 Mo v eMultiple R0 R3, (SP) Sa v e registers. 2112 Mo v e 8(FP),R0 Get first parameter. Mo v e 12(FP),R1 Get second parameter. . . . – Mo v e P ARAM3, (SP) Place a parameter on stack. 2160 Call SUB2 2164 Mo v e (SP)+,R2 P op SUB2 result in to R2. . . . Mo v e R3,8(FP) Place answ er on stack. – Mo v eMultiple (SP)+,R0 R3 Restore registers. Mo v e (SP)+,FP Restore frame p oin ter register. Return Return to Main program. Second subroutine – 3000 SUB2 Mo v e FP , (SP) Sa v e frame p oin ter register. Mo v e SP ,FP Load the frame p oin ter. – – Mo v eMultiple R0 R1, (SP) Sa v e registers R0 and R1. Mo v e 8(FP),R0 Get the parameter. . . . Mo v e R1,8(FP) Place SUB2 result on stack. – Mo v eMultiple (SP)+,R0 R1 Restore registers R0 and R1. Mo v e (SP)+,FP Restore frame p oin ter register. Return Return to Subroutine 1. Figure 2.28. Nested subroutines.
[R1] from SUB1 [R0] from SUB1 Stack frame [FP] from SUB1 FP for second subroutine 2164 param3 [R3] from Main [R2] from Main [R1] from Main Stack [R0] from Main frame for FP [FP] from Main first subroutine 2012 param1 param2 Old TOS Figure 2.29. Stack frames for Figure 2.28.
C R0 0 . . . before: 0 0 1 1 1 0 0 1 1 . . . after: 1 1 1 0 0 1 1 0 0 (a) Logical shift left LShiftL #2,R0 0 R0 C . . . 0 1 1 1 0 0 1 1 before: 0 . . . after: 0 0 0 1 1 1 0 0 1 (b) Logical shift r ight LShiftR #2,R0 R0 C . . . before: 0 1 0 0 1 1 0 1 0 . . . after: 1 1 1 0 0 1 1 0 1 (c) Ar ithmetic shift r ight AShiftR #2,R0 Figure 2.30. Logical and arithmetic shift instructions.
C R0 . . . before: 0 1 1 1 0 0 1 1 0 . . . after: 1 1 1 0 0 1 1 0 1 (a) Rotate left without carr y RotateL #2,R0 C R0 . . . before: 0 0 1 1 1 0 0 1 1 . . . after: 1 1 1 0 0 1 1 0 0 (b) Rotate left with carr y RotateLC #2,R0 R0 C . . . 0 before: 0 1 1 1 0 0 1 1 . . . after: 1 1 0 1 1 1 0 0 1 (c) Rotate r ight without carr y RotateR #2,R0 R0 C . . . 0 1 1 1 0 0 1 1 0 before: . . . 1 after: 1 0 0 1 1 1 0 0 (d) Rotate r ight with carr y RotateRC #2,R0 Figure 2.32. Rotate instructions.
– – (j = n 1; j > 0; j = j 1) for – – { ( k = j 1; k > = 0; k = k 1 ) for { (LIST[ k ] > LIST[ j ]) if { TEMP = LIST[ k ]; LIST[ k ] = LIST[ ]; j LIST[ j ] = TEMP; } } } (a) C-language program for sorting Mo v e #LIST,R0 Load LIST in to base register R0. Mo v e N,R1 Initialize outer lo op index – Subtract #1,R1 register R1 to j = n 1. OUTER Mo v e R1,R2 Initialize inner lo op index – Subtract #1,R1 register R2 to k = j 1. Mo v eByte (R0,R1),R3 Load LIST( j ) in to R3, whic h holds current maxim um in sublist. INNER CompareByte R3,(R0,R2) If LIST( k ) [R3], Branc h 0 NEXT do not exhange. Mo v eByte (R0,R2),R4 Otherwise, exchange LIST( k ) Mo v eByte R3,(R0,R2) with LIST( j ) and load Mo v eByte R4,(R0,R1) new maxim um in to R3. Mo v eByte R4,R3 Register R4 serv es as TEMP . NEXT Decremen t R2 Decremen t index registers R2 and Branc h 0 INNER R1, whic h also serv e Decremen t R1 as lo op coun ters, and branc h Branc h > 0 OUTER bac k if lo ops not finished. (b) Assembly language program for sorting Figure 2.34. A byte-sorting program using a straight-selection sort.
Memory Key Link Data field address field field (ID) (Test scores) 1 word 1 word 3 words First 2320 27243 1040 Head record Second 1040 28106 1200 record Third 1200 28370 2880 record • • • Second last 2720 40632 1280 record Last 1280 47871 0 Tail record Figure 2.36. A list of student test scores organized as a linked list in memory.
INSER TION Compare #0, RHEAD Branch>0 HEAD ne w record Mo v e RNEWREC, RHEAD becomes a not empty one-entry list Return Compare (RHEAD), (RNEWREC) HEAD Branch>0 SEARCH ne w record Mo v e RHEAD, 4(RNEWREC) insert ne w record becomes some where after Mo v e RNEWREC, RHEAD ne w head current head Return Mo v e RHEAD, RCURRENT SEARCH Mo v e 4(RCURRENT), RNEXT LOOP Compare #0, RNEXT Branch=0 T AIL ne w record becomes ne w tail Compare (RNEXT), (RNEWREC) Branch<0 INSER T insert ne w record in Mo v e RNEXT , RCURRENT an interior position Branch LOOP INSER T Mo v e RNEXT , 4(RNEWREC) Mo v e RNEWREC, 4(RCURRENT) T AIL Return Figure 2.37. A subroutine for inserting a new record into a linked list.
8 7 7 10 OP code Source Dest Other info (a) One-word instruction OP code Source Dest Other info Memory address/Immediate operand (b) Two-word instruction OP code R i R j R k Other info (c) Three-operand instruction Figure 2.39. Encoding instructions into 32-bit words.
T able 2.1 Generic addressing modes Name Assem bler syn tax Addressing function Immediate #V alue Op erand = V alue Register R i EA = R i Absolute (Direct) LOC EA = LOC Indirect (R i ) EA = [R i ] (LOC) EA = [LOC] Index X(R i ) EA = [R i ] + X Base with index (R i ,R j ) EA = [R i ] + [R j ] Base with index X(R i ,R j ) EA = [R i ] + [R j ] + X and offset Relative X(PC) EA = [PC] + X Autoincremen t (R i )+ EA = [R i ] ; Incremen t R i Autodecrement (R i ) Decremen t R i ; EA = [R i ] EA = effectiv e address V alue = a signed n um b er