370 likes | 536 Views
CPS 4150 Computer Organization Chapter 2-2. Fall 2006 Ching-Song Don Wei. 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 ). operands.
E N D
CPS 4150 Computer OrganizationChapter 2-2 Fall 2006 Ching-Song Don Wei
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). operands Absolute mode Destination, in memory Register 0, source
2.6 Assembly Language • Immediate mode ADD #5,R3 Or ADDI 5,R3 • Indirect mode MOVE #5,(R2) OR MOVEI 5,(R2)
2.6.1 Assembly Directives • For example: SUM EQU 200 • This directive informs assembler that the name SUM should be replaced by the value 200. • No corresponding object code for this
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.
Where to place data block Memory Addressing address or data lab el Op eration information First data Assem bler directiv es SUM EQU 200 ORIGIN 204 N D A T A W ORD 100 Reserve 400 bytes NUM1 RESER VE 400 ORIGIN 100 Statemen ts that ST AR T MO VE N,R1 generate MO VE #NUM1,R2 mac hine CLR R0 Starting address instructions LOOP ADD (R2),R0 ADD #4,R2 DEC R1 BGTZ LOOP MO VE R0,SUM Assem bler directiv es RETURN END ST AR T Figure 2.18. Assembly language representation for the program in Figure 2.17.
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.
2.7 Basic I/O operations • 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.
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.
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 READWAIT Branch 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.
2.7 Basic I/O operations • For DATOUT and SOUT in display READWAIT Branch 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.
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 b3in device registers INSTATUS and OUTSTATUS corresponds to SIN and SOUT, respectively.
2.7 Basic I/O operations • The implementation of READ and WRITE operations can be implemented by machine instruction sequence: READWAIT Testbit #3,INSTATUS Branch=0 READWAIT MoveByte DATAIN,R1 WRITEWAIT Testbit #3,OUTSTATUS Branch=0 WRITEWAIT MoveByte R1,DATAOUT
Mo v e #LOC,R0 Initialize p oin ter register R0 to p oin t to the address of the first lo cation in memory where the c haracters are to b e stored. READ T estBit #3,INST A TUS W ait for a c haracter to b e en tered Branc h=0 READ in the k eyb oard buffer D A T AIN. Mo v eByte D A T AIN,(R0) T ransfer the c haracter from D A T AIN in to the memory (this clears SIN to 0). ECHO T estBit #3,OUTST A TUS W ait for the displa y to b ecome ready . Branc h=0 ECHO Mo v eByte (R0),D A T A OUT Mo v e the c haracter just read to the displa y buffer register (this clears SOUT to 0). Compare #CR,(R0)+ Chec k if the c haracter just read is CR (carriage return). If it is not CR, then Branc h READ branc h bac k and read another c haracter. 0 Also, incremen t the p oin ter to store the next c haracter. Figure 2.20. A program that reads a line of characters and displays it.
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.
2.8 Stacks and Queues 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.
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 Move NEWITEM,(SP)
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
2.8 Stacks and Queues • For autoincrement and Autodecrement addressing mode: Move NEWITEM,-(SP) Move (SP)+,ITEM
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.
2.8 Stacks and Queues • Check the stack location before pop and push operation.
SAFEPOP Compare #2000,SP Chec to see if the stac k p oin ter con tains Branc h > 0 EMPTYERR OR an address v alue greater than 2000. If it do es, the stac k is empt y . Branc h to the routine EMPTYERR OR for appropriate action. Mo v e (SP)+,ITEM Otherwise, p op the top of the stac k in to memory lo cation ITEM. (a) Routine for a safe pop operation SAFEPUSH Compare #1500,SP Chec k to see if the stac k p oin ter Branc h 0 FULLERR OR con tains an address v alue equal to or less than 1500. If it do es, the stac k is full. Branc h to the routine FULLERR OR for appropriate action. – Mo v e NEWITEM, (SP) Otherwise, push the elemen t in memory lo cation NEWITEM on to the stac k. (b) Routine for a safe push operation Figure 2.23. Checking for empty and full errors in pop and push operations.
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:
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
2.9 Subroutines 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.
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.
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.
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.
2.9.2 Parameter Passing Calling program Mo v e N,R1 R1 serv es as a coun ter. Mo v e #NUM1,R2 R2 p oints to the list. Call LIST ADD Call subroutine. Mo v e R0,SUM Sa v e result. . . . Subroutine LIST ADD Clear R0 Initialize sum to 0. LOOP Add (R2)+,R0 Add en try from list. Decremen t R1 Branc h LOOP > 0 Return Return to calling program.
2.9.2 Parameter Passing • Using stack is more flexible
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.
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.
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.
2.9.3 The Stack Frame 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.
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.
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