Presentation is loading. Please wait.

Presentation is loading. Please wait.

Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture Instructions: Part the Last.

Similar presentations


Presentation on theme: "Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture Instructions: Part the Last."— Presentation transcript:

1 Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture Instructions: Part the Last

2 OCC - CS/CIS CS116-Ch00-Orientation 2 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 2 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Brief review Different Types Instructions – R-Type – I-Type – J-Type Assembly Instructions – Arithmetic – Memory – Branching

3 OCC - CS/CIS CS116-Ch00-Orientation 3 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 3 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Today Support for procedures – Assembler and Hardware Shifting Data other than numbers And Last, But Certainly Not Least... Actually programming in assembly!

4 OCC - CS/CIS CS116-Ch00-Orientation 4 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 4 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Procedures AKA Subroutines Reusable chunks of code Used to structure programs – Easier to read – Easier to understand Leave no trace

5 OCC - CS/CIS CS116-Ch00-Orientation 5 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 5 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Properties of Procedures Have the following properties: – Parameters- the input to the procedure – Code Block- the actual instructions of the procedure – Return value- the result of the procedure – Separate from the current program location Often need: – Storage for local variables

6 OCC - CS/CIS CS116-Ch00-Orientation 6 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 6 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) How do we call a procedure? 1.Place parameters someplace procedure can access them 2.Transfer execution to the procedure location 3.Acquire local storage resources 4.Execute the code 5.Place the result someplace the original code can access 6.Return control to the original code

7 OCC - CS/CIS CS116-Ch00-Orientation 7 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 7 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) How does the hardware help? Well, registers are fast... – MIPS reserves $a0-$a3 for passing parameters – $v0 and $v1 are reserved for return values – $ra is reserved for the address to return control to

8 OCC - CS/CIS CS116-Ch00-Orientation 8 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 8 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) And what about instructions? Because the procedure's code is not close by – Use a jump instruction MIPS provides a specialized jump instructions – Jump and link instruction jal ProcedureAddress Link means store the address we are coming from in $ra – Called the return address – Jump register instruction jr $ra We can do this at the end of a procedure to return

9 OCC - CS/CIS CS116-Ch00-Orientation 9 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 9 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) A short example Proc1:add $t0, $a0, $a1 mul $t0, $0, $a3 sub $t0, $a4, $t0 add $v0, $t0, $zero jr $ra Main:li $a0, 12 li $a1, 42 li $a3, 123 li $a4, 10120 jal Proc1 (do some more instructions) Start Here

10 OCC - CS/CIS CS116-Ch00-Orientation 10 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 10 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Great, but... What happens if: – More parameters than 4? – Need to call another procedure? Destroy $ra as well as parameters ($a0-$a4) – Need storage for more than 8 temporaries? Answer – We need non-register memory to store information

11 OCC - CS/CIS CS116-Ch00-Orientation 11 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 11 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Ahh, the Stack Recall, the stack is used for local storage – LIFO structure- Last In First Out Operations – Push values on Adds data to stack – Pop top the value off Removes data from the stack Keep in mind – Stack grows downward in memory

12 OCC - CS/CIS CS116-Ch00-Orientation 12 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 12 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Making it work Need to know – Where the top of the stack is MIPS: Stack Pointer $sp – How to put data on stack Use $sp as the offset register and use – Store word instruction – Load word insrtuction Use arithmetic to allocate/deallocate space – Add/subtract to/from $sp to adjust stack

13 OCC - CS/CIS CS116-Ch00-Orientation 13 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 13 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) The Stack Push example subi $sp, $sp, 12 sw $a0, 8($sp) sw $a1, 4($sp) sw $t0, 0($sp) Pop example lw $t0, 0($sp) lw $a1, 4($sp) lw $a0, 8($sp) addi $sp, $sp, 12 Top of memory $sp -12 $t0 $a1 $a0 Top of memory $sp +12 $t0 $a1 $a0

14 OCC - CS/CIS CS116-Ch00-Orientation 14 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 14 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Using the stack Push parameters on the stack Push registers that need to be saved – $s0..$s7 – NOT $t0..$t7 Allocate local temporary storage – Subtract appropriate amount from $sp When calling another procedure – Save $ra

15 OCC - CS/CIS CS116-Ch00-Orientation 15 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 15 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) But if stack changes... How do we access local arrays, etc? – Use a register to keep track of where they are located Frame Pointer – Register $fp – Set to value of $sp when procedure starts – Change $sp as much as we want, but always have a reference point Handled by the program, not by the processor

16 OCC - CS/CIS CS116-Ch00-Orientation 16 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 16 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Stack Frame $sp $fp $sp $fp $sp $fp Saved arg Regs Saved $ra Saved regs Local storage

17 OCC - CS/CIS CS116-Ch00-Orientation 17 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 17 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Shifting Operations that move bits – Shift Left Move all the bits in register left a certain amount – Shift Right Move all the bits in the register right a certain amount Examples – Shift Left sll $t0, $t1, 3 – Shift Right srl $t0, $t1, 2 000111100011000 00110

18 OCC - CS/CIS CS116-Ch00-Orientation 18 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 18 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Shifting Notice – Shift left by 1 is like muliplying by 2 – Shift right by 2 is like dividing by 2 If bits are shifted past the 32 nd (or 0 th ) bit – They are just shifted away, to be lost forever

19 OCC - CS/CIS CS116-Ch00-Orientation 19 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 19 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Rotating Similar to shifting, except – Bits shifted off the end are wrapped around to the other side – Example: rotate left 3 00110 10001

20 OCC - CS/CIS CS116-Ch00-Orientation 20 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 20 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) More than numbers Computers not calculators – Work with not just numbers – But also text Deal with characters – Encoded with ASCII – Combined into sequences of strings Use the load/store byte instruction to get data lb $t0, 1($s0) sb $t0, 1($s0) Use load address to get the address of data la $t0, DataLabel (Address)

21 OCC - CS/CIS CS116-Ch00-Orientation 21 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 21 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) String representations Reserve the first position to indicate length – Can only store 255 characters in the string – Pascal Use an external variable – Requires more memory – Java Use a special character to terminate the string – C uses the 0 (zero) character Null terminated strings

22 OCC - CS/CIS CS116-Ch00-Orientation 22 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 22 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Finally, we are ready to code Syntax – One instruction per line – # starts a comment Continues until the end of the line – Identifiers Alphanumeric, '_' and '.' Cannot start with a number – Labels Identifiers at the start of a line followed by ':'

23 OCC - CS/CIS CS116-Ch00-Orientation 23 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 23 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) More Language Syntax – Strings enclosed in “ ” \n A new line \t A tab character \” A quotation mark – Numbers Base 10 Hexadecimal – 0xABCD

24 OCC - CS/CIS CS116-Ch00-Orientation 24 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 24 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Directives Special commands for the assembler Usually starts with '.' – Example:.asciiz“The sum from 0..10 is %d\n” Is equivalent to:.byte84, 104, 101, 32, 115, 117, 109, 32.byte102, 114, 111, 109, 32, 48, 32, 46.byte46, 32, 49, 48, 32, 105, 115, 32.byte37, 100, 10, 0 – Note: 10 is ASCII code for line feed 0 is ASCII code for the NULL character

25 OCC - CS/CIS CS116-Ch00-Orientation 25 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 25 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Directives Some example directives:.asciiz str# Store null-terminated string.byte b1,..., bn# Store in successive bytes.data # Store in data segment.float fl1,..., fn # Store floating-pt. single precision.globl sym # sym can be referenced from other files.space n # Allocate n-bytes of space in data seg..text # Put text items in text segment.word w1,..., wn # Full word in successive memory

26 OCC - CS/CIS CS116-Ch00-Orientation 26 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 26 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) I/O Need to be able to read and write data – For the user SPIM, this is accomplished with syscall In general, uses interrupt – Stalls processor state – Tells the processor to execute a procedure associated with the interrupt. – In PCs, this is what an IRQ is

27 OCC - CS/CIS CS116-Ch00-Orientation 27 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 27 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Using syscall (interrupts) Put a given constant into register $v0 Fill in the set of argument registers Execute the syscall instruction The result will be in register $v0 Look on page A-49 of the book for available service codes

28 OCC - CS/CIS CS116-Ch00-Orientation 28 1998 Morgan Kaufmann Publishers ( Augmented & Modified by M.Malaty) 28 OCC-CS 116 Fall 2003 1998 Morgan Kaufmann Publishers (Augmented & Modified by M.Malaty and M. Beers) Example syscall.data var:.asciiz “This is a string\n”.text.globl main main:li $v0, 4 la $a0, var syscall


Download ppt "Orange Coast College Business Division Computer Science Department CS 116- Computer Architecture Instructions: Part the Last."

Similar presentations


Ads by Google