Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei.

Similar presentations


Presentation on theme: "CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei."— Presentation transcript:

1 CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei

2 2.6 Assembly Language Assembly program = mnemonics + syntax Assembly program (source program) assembler  object program MOVE R0,SUM Mnemonic MOVE represents the binary pattern (OP code). Register 0, source operands Destination, in memory  Absolute mode

3 2.6 Assembly Language Immediate mode ADD #5,R3 Or ADDI 5,R3 Indirect mode MOVE #5,(R2) OR MOVEI 5,(R2)

4 2.6.1 Assembly Directives For example: SUMEQU200 This directive informs assembler that the name SUM should be replaced by the value 200. No corresponding object code for this

5 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 Decrement Add Move #NUM1,R2 N,R1Move Branch>0

6 MemoryAddressing addressordata labelOperationinformation AssemblerdirectivesSUMEQU200 ORIGIN204 NDATAWORD100 NUM1RESERVE400 ORIGIN100 StatementsthatSTARTMOVEN,R1 generateMOVE#NUM1,R2 machineCLRR0 instructionsLOOPADD(R2),R0 ADD#4,R2 DECR1 BGTZLOOP MOVER0,SUM AssemblerdirectivesRETURN ENDSTART Figure 2.18. Assembly language representation for the program in Figure 2.17. Where to place data block First data Reserve 400 bytes Starting address

7 2.6.2 Execution of Program Two pass asembler – –As the assembler scans through a source program, it keeps track of all names and the numerical values that correspond to them in a symbol table. – –When a name appears a second time, it is replaced with its value from the table. – –But, when a name appears as an operand before it is given a value, it needs two-pass assembler.

8 2.7 Basic I/O operations

9 Register DATAIN, a status control flag, SIN, is set to 1 to inform the processor to read the contens of DATAIN. When the character is transferred to the processor, SIN is automatically cleared to 0. Same as in Display, when SOUT equals 1, the display is ready to receive character.

10 2.7 Basic I/O operations The processor monitors SOUT and when SOUT is set to 1, the processor transfers a character code to DATAOUT. The Buffer registers, DATAIN, DATAOUT, SIN and SOUT are part of circuitry called device interface.

11 2.7 Basic I/O operations Instructions to that can check the state of the status flags and transfer data between the processor and the I/O. For DATAIN and SIN in Keyboard READWAITBranch to READWAIT if SIN = 0 input from DATAIN to R1 Initial state of SIN is 1. The branch operation normally implemented by two machine instruction.

12 2.7 Basic I/O operations For DATOUT and SOUT in display READWAITBranch to READWAIT if SOUT = 0 input from R1 to DATAOUT Initial state of SOUT is 0 The branch operation normally implemented by two machine instruction.

13 2.7 Basic I/O operations In memory-mapped I/O, which some memory address values are used to refer to peripheral device buffer registers such as DATAIN and DATAOUT. Include bit b 3 in device registers INSTATUS and OUTSTATUS corresponds to SIN and SOUT, respectively.

14 2.7 Basic I/O operations The implementation of READ and WRITE operations can be implemented by machine instruction sequence: READWAITTestbit#3,INSTATUS Branch=0READWAIT MoveByteDATAIN,R1 WRITEWAIT Testbit#3,OUTSTATUS Branch=0WRITEWAIT MoveByteR1,DATAOUT

15 Move#LOC,R0InitializepointerregisterR0topointtothe addressofthefirstlocationinmemory wherethecharactersaretobestored. READTestBit#3,INSTATUSWaitforacharactertobeentered Branch=0READinthekeyboardbufferDATAIN. MoveByteDATAIN,(R0)TransferthecharacterfromDATAINinto thememory(thisclearsSINto0). ECHOTestBit#3,OUTSTATUSWaitforthedisplaytobecomeready. Branch=0ECHO MoveByte(R0),DATAOUTMovethecharacterjustreadtothedisplay bufferregister(thisclearsSOUTto0). Compare#CR,(R0)+CheckifthecharacterjustreadisCR (carriagereturn).IfitisnotCR,then Branch 00 READbranchbackandreadanothercharacter. Also,incrementthepointertostorethe nextcharacter. Figure 2.20. A program that reads a line of characters and displays it.

16 2.8 Stacks and Queues In order to organize the control and information linkage between the main and subroutine, a data structure, stack is used. LIFO last in first out with push and pop operations. The first element is placed in location of BOTTOM.

17 2.8 Stacks and Queues Figure 2.21. A stack of words in the memory. register Stack pointer 17 BOTTOM 0 SP Current top element element Bottom Stack 2 k 1- 739 43 28-

18 2.8 Stacks and Queues A processor register is used to keep track of the address of the element of the stack that is at the top at any time. For a byte-addressable memory with 32-bit word length, the push operation : Subtract#4,SP MoveNEWITEM,(SP)

19 2.8 Stacks and Queues Subtract 4 from SP and places the new information in location pointed by SP Move word from location NEWITEM onto the top of the stack. The pop operation: Move(SP),ITEM Add#4,SP

20 2.8 Stacks and Queues For autoincrement and Autodecrement addressing mode: MoveNEWITEM,-(SP) Move(SP)+,ITEM

21 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- - -

22 2.8 Stacks and Queues Check the stack location before pop and push operation.

23 SAFEPOPCompare#2000,SPChectoseeifthestackpointercontains Branch>0EMPTYERRORanaddressvaluegreaterthan2000.Ifit does,thestackisempty.Branchtothe routineEMPTYERRORforappropriate action. Move(SP)+,ITEMOtherwise,popthetopofthestackinto memorylocationITEM. SAFEPUSHCompare#1500,SPChecktoseeifthestackpointer Branch0FULLERRORcontainsanaddressvalueequal toorlessthan1500.Ifitdoes,the stackisfull.Branchtotheroutine FULLERRORforappropriateaction. MoveNEWITEM,(SP)Otherwise,pushtheelementinmemory locationNEWITEMontothestack.  – Figure 2.23. Checking for empty and full errors in pop and push operations. (b) Routine for a safe push operation (a) Routine for a safe pop operation

24 2.9 Subroutines The way in which a computer makes it possible to call and return from subroutines is referred to as its subroutine linkage. The returning address is stored in a register called link register. The subroutine Call instruction is just a special branch instruction that performs the following operations:

25 2.9 Subroutines – –Store the contents of the PC in the link register – –Branch to the target address specified by the instruction The Return instruction: – –Branch to the address contained in the link register

26 2.9 Subroutines ReturnCall Figure 2.24. Subroutine linkage using a link register. 1000 204 Link PC Return 1000 location Memory Calling program Memory location 200 204 Call SUB next instruction Subroutine SUB first instruction

27 2.9.1 Nested Subroutines Subroutine nesting: – –One subroutine calls another anther subroutine. The return addresses are generated and used in a last-in-first-out order. A stack, called processor stack, can be used for this purpose. A register is designated as the stack pointer, SP.

28 2.9.1 Nested Subroutines The Call instruction pushes the contents of the PC onto the processor stack and loads the subroutine address into the PC. The Return instruction pops the return address from the processor stack into the PC.

29 2.9.2 Parameter Passing Many ways to implement parameter passing: – –Placed in processor registers – –Placed in processor stack If processor register is used: – –The register can be accessed by subroutine and calling program.

30 2.9.2 Parameter Passing Callingprogram MoveN,R1R1servesasacounter. Move#NUM1,R2R2pointstothelist. CallLISTADDCallsubroutine. MoveR0,SUMSaveresult.... Subroutine LISTADDClearR0Initializesumto0. LOOPAdd(R2)+,R0Addentryfromlist. DecrementR1 Branch >0 LOOP Return tocallingprogram.

31 2.9.2 Parameter Passing Using stack is more flexible

32 Assumetopofstackisatlevel1below. Move#NUM1,(SP)Pushparametersontostack. MoveN,(SP) CallLISTADDCallsubroutine (topofstackatlevel2). Move4(SP),SUMSaveresult. Add#8,SPRestoretopofstack (topofstackatlevel1).... LISTADDMoveMultipleR0R2,(SP)Saveregisters (topofstackatlevel3). Move16(SP),R1Initializecounterton. Move20(SP),R2Initializepointertothelist. ClearR0Initializesumto0. LOOPAdd(R2)+,R0Addentryfromlist. DecrementR1 Branch >0 LOOP MoveR0,20(SP)Putresultonthestack. MoveMultiple(SP)+,R0R2Restoreregisters. 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 –

33 2.9.3 The Stack Frame The space – stack frame constitute a private work space for the subroutine, created at the time the subroutine is entered and freed up when the subroutine returns control to the calling program. Frame pointer (FP) is for convenient access to the parameters passed to the subroutine and to the local memory variables used by subroutine only.

34 2.9.3 The Stack Frame The parameter can be accessed by using addresses 8(FP), 12(FP), … The content of FP remains fixed throughout the execution of the subroutine. The content of SP always points to the top element in the stack.

35 2.9.3 The Stack Frame Figure 2.27. A subroutine stack frame example. SP (stack pointer) FP (frame pointer) saved [R1] saved [R0] Stack frame for called subroutine Return address localvar3 localvar2 localvar1 saved [FP] Old TOS param2 param1 param3 param4 (top-of-stack)

36 2.9.3 The Stack Frame Four parameters are pushed into stack Return address of calling program is pushed into stack Saving the current content of FP Copy the SP to the FP Allocate the space for local variables Saving the current content of R1 and R2 into stack.

37 2.9.3 The Stack Frame Subroutine is now executing its task. When subroutine is done, the subroutine pops the saved value of R1 and R0 back into those registers, removes the local variables from the stack frame Old value of FP  FP Return address will be used for returning to calling program


Download ppt "CPS 4150 Computer Organization Chapter 2-2 Fall 2006 Ching-Song Don Wei."

Similar presentations


Ads by Google