Download presentation
Presentation is loading. Please wait.
Published byKristian Hubbard Modified over 9 years ago
2
Ch2b- 2 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Thanks for all the Memory! When 32 registers just won’t do. Many times (almost all the time, actually), you can’t fit all of your data into 32 measly registers. What do you do? Put some (most) of the data in main memory. Remember: Smaller is faster. Registers: Small and Fast Memory: Big and Slow In MIPS, all operations (i.e. arithmetic) are done on registers, only. Memory is used only for storing what won’t fit in registers. In MIPS, all operations (i.e. arithmetic) are done on registers, only. Memory is used only for storing what won’t fit in registers.
3
Ch2b- 3 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Loading and Storing So you’ve got some data in memory. Big deal. You need it in a register to do anything useful. You need to load a register with a value from memory. lw $10, 1000($0)# copy memory location 1000 to $10 Load Word - Loads a whole 32-bit word This value is added to 1000 - for now, it is zero Say you’ve added 1 to register $10 (now it has the value 44). Now you want to put it back in memory again. You need to store the register’s value back to memory. sw $10, 1000($0)# copy $10 to memory location 1000 3288 0996 43 1000 234 1004 Afterwards, $10 has the value 43 44
4
Ch2b- 4 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Load/Store Format What instruction format do LW and SW have? lw $5, 240($9) # load M[240+$9] into $5 Needs Opcode Source register ($9) Immediate Operand (240) Destination register ($5) Opcode for LW: 35 Think Regularity! Opcode RS RT Immediate Data 6 bits5 bits 16 bits 35 9 5 240 100011 01001 00101 0000 0000 1111 0000 I-Type Instruction Opcode for SW: 43 Hmmm, we’ve seen this before....
5
Ch2b- 5 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Aside: Load and Store Architectures The only way to communicate with memory is through a LW or SW instruction If you want to operate on memory, you have to use at least three instructions (usually) lw $15, 4500($0) # load M[4500] into $15 add $15, $15, $3 # add $3 to $15 sw $15, 4500($0) # store $15 back into M[4500] It doesn’t have to be this way Contrast this with the Motorola 68000 ADD D3, 4500 ; add register D3 to M[4500] Is the grass greener on the other side? MIPS: Takes more, simpler instructions... RISC MC68000: Takes fewer, complex instructions... CISC
6
Ch2b- 6 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Assembler directives Somehow, we’ve got to get data into memory User input Involves system calls (we’ll get to that later) Constant data Constant data is data that is in memory before our program starts executing Machine-language instructions don’t give much help The only way is to use Immediate instructions The assembler helps us here! Assembler directives are special commands to the assembler. The most common directives put data into memory.
7
Ch2b- 7 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University 0100 0200 Buffer: Buffer+ 4:.word Assembler Directive Buffer:.word01, 02 Label: A name for this memory to go by. Acts as a variable name. Label: A name for this memory to go by. Acts as a variable name..word: Directive to store words in memory here. Data to be stored. lw $12, Buffer($0) # $12 <-- 00 00 00 01 addi $10, $0, 4 # $10 <-- 4 lw $13, Buffer($10) # $13 <-- 00 00 00 02 Remember: Words are 4 bytes each! Loads from Buffer+0Loads from Buffer+4
8
Ch2b- 8 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University The Assembler Location Counter The assembler keeps track of where to put things by using a location counter. The location counter just points to the memory location to put the “next” item. buffer1:.word12 buffer2:.word3, 4, 0x20, 0x5 add$9, $0, $0 firstld:lw$8, buffer1($9) addi$9, $9, 4 secld:lw$10, buffer2($9) For this example, assume the location counter starts at 4000 4000: 4004: 4020: 4024: 4028: 4032: 40044008 4012 4016 Hex Constants a denoted by the “0x” prefix buffer1 = 4000 buffer2 = 4004 firstld = 4024 secld = 4032 Label Table Loc. Ctr. Instructions – stored in ordinary memory. Execute by telling the CPU to start running at location 4020.
9
Ch2b- 9 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Other Memory Assembler Directives borg:.byte33, 22, 12, 10, 8, 1 greeting:.asciiz“Resistance is futile.” greeting2:.ascii“You will be assimilated.” 69736552 greeting: 02E656C... Null-terminated 20756F59 --2E6465... greeting2:.byte - reserves bytes in memory.asciiz - reserves Null- terminated ASCII chars.ascii - reserves ASCII characters (no NULL) 10122233 borg: ??18 Resi le.
10
Ch2b- 10 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Meeting all your needs for space Sometimes, we need to allocate (empty) space to be used later. inputbuffer:.space100 Allocates 100 bytes of space for an input buffer. Space allocated this way is just reserved by the assembler. You have to make your own use of it. addi$12, $0, 6 sw$12, inputbuffer($0) # stores 6 in buffer.align 2 inputbuffer:.space100 If the space is to be used as words (with LW,SW), make sure it is aligned to a multiple of 2 2 using the.align directive
11
Ch2b- 11 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Our first program! # This is our first program! Yeah!.data Tonto:.word0x44, 0x22.text main: add$9, $0, $0 # clear $9 lw$8, Tonto($9) # put Tonto[0] in $8 addi$9, $9, 4 # increment $9 lw$10, Tonto($9) # put Tonto[1] in $10 addi$v0,$0,10 syscall.data means that data follows.text means that code follows main: tells SPIM where to start these two instructions end the program
12
Ch2b- 12 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University SPIM The SPIM simulator is a MIPS simulator Allows development and debugging of MIPS programs without having to run on a MIPS CPU QTSPIM (newer, many platforms) or PCSPIM at http://sourceforge.net/projects/spimsimulator/files/ Help on SPIM Appendix A of your textbook Handouts and sample programs Look under Resources on the course web page
13
Ch2b- 13 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Pseudoinstructions Some “missing” instructions are commonly composed of others The assembler “implements” these by allowing the “missing” instructions to be entered in assembly code. When machine code is generated, the pseudoinstructions are converted to real instructions. move$5, $3add$5, $3, $0 neg$8, $9sub$8, $0, $9 li$8, 44addi$8, $0, 44 or ori$8, $0, 44
14
Ch2b- 14 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Pseudoinstructions for branches Branches can be nasty to figure out SPIM provides several pseudoinstructions for branches blt$3, $4, destslt$1, $3, $4 bne$1, $0, dest bgt$3, $4, destslt$1, $4, $3 bne$1, $0, dest $3 > $4 same as $4 < $3 ble$3, $4, destslt$1, $4, $3 beq$1, $0, dest bge$3, $4, destslt$1, $3, $4 beq$1, $0, dest $3 >= $4 is the opposite of $3 < $4 $3 $4
15
Ch2b- 15 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University SPIM I/O SPIM I/O uses the SYSCALL pseudoinstruction Set up parameters Place correct code in $v0 Execute SYSCALL To print the value in $t3: move $a0, $t3 li $v0, 1 syscall To display a string prompt:.asciiz “hello world” la $a0,prompt li $v0, 4 syscall ActionCode (in $v0)Parameters Print an Integer1$a0 = value to print Print a String4$a0 = location of string Input an Integer5(after syscall) $v0 contains integer Input a String8$a0 = location of buffer, $a1 = length Exit program10
16
Ch2b- 16 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Data Structures - Arrays A single-dimensional array (vector) is a simple linear data structure 2000 2004 2008 2012 2016 2020 2024 2028 int A[5]; /* integers are 4 bytes each */ start of array (2004 in this example) A[0] A[1] A[2] A[3] A[4] For 4-byte integers: Location of A[n] = Start + n*4; For data items of size s bytes: Location of A[n] = Start + n*s; To declare an array named A with 40 bytes: A:.space 40 A is the address of element 0. The program must do all computations needed to access any other elements.
17
Ch2b- 17 EE/CS/CPE 3760 - Computer Organization Seattle Pacific University Accessing data structures in memory Assume that the variable List points to the beginning of an array of 32-bit integers. List=6000 123 6000 3288 6004 43 6008 1 6012 45 6016... Note: Memory addresses refer to 8-bit bytes! We usually reference 32-bit words. All lw/sw instructions must use an address that is a multiple of 4! To get proper index, have to multiply by 4. Note: Memory addresses refer to 8-bit bytes! We usually reference 32-bit words. All lw/sw instructions must use an address that is a multiple of 4! To get proper index, have to multiply by 4. Move List[0] into $3: lw $3, List($0) # $3 <-- List[0] Move List[1] into $4: addi $8, $0, 4 # $8 <-- 4 lw $4, List($8) # $4 <-- List[1] addi $8, $0, 16 # $8 <-- 16 lw $5, List($8) # $5 <-- List[4] Move List[4] into $5: List and contents of $8 are added together to form address List[0] List[1] List[2] List[3] List[4]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.