ch2b- 2 ee/cs/cpe 3760 - computer organization seattle pacific university thanks for all the...
TRANSCRIPT
Ch2b- 2EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Thanks for all the Memory!When 32 registers just won’t do.When 32 registers just won’t do.
Many times (almost all the time, actually), you can’t fit all of yourdata into 32 measly registers.
What do you do? Put some (most) of the data in main memory.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 whatwon’t fit in registers.
In MIPS, all operations (i.e. arithmetic)are done on registers, only.
Memory is used only for storing whatwon’t fit in registers.
Ch2b- 3EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Loading and StoringSo you’ve got some data in memory. Big deal. You need it in a registerto do anything useful.
You need to load a register with a value from memory.
lw $10, 1000($0) # copy memory location 1000 to $10lw $10, 1000($0) # copy memory location 1000 to $10
Load Word - Loads a whole 32-bit wordLoad Word - Loads a whole 32-bit word This value is added to 1000 - for now, it is zeroThis 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 sw $10, 1000($0) # copy $10 to memory location 1000
32880996431000
2341004
Afterwards, $10 has the value 43Afterwards, $10 has the value 43
44
Ch2b- 4EE/CS/CPE 3760 - Computer OrganizationSeattle 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)
Opcodefor LW: 35Opcodefor LW: 35
ThinkRegularity!ThinkRegularity!
Opcode RS RT Immediate Data
6 bits 5 bits 5 bits 16 bits
35 9 5 240
100011 01001 00101 0000 0000 1111 0000
I-Type InstructionI-Type Instruction
Opcodefor SW: 43Opcodefor SW: 43
• Hmmm, we’ve seen this before....
Ch2b- 5EE/CS/CPE 3760 - Computer OrganizationSeattle 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
Ch2b- 6EE/CS/CPE 3760 - Computer OrganizationSeattle 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.
Ch2b- 7EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
01000000
02000000
Buffer:Buffer+ 4:
.word Assembler Directive
Buffer: .word 01, 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..word: Directive to store words in memory here.
Data to be stored.Data to be stored.
lw $12, Buffer($0) # $12 <-- 00 00 00 01addi $10, $0, 4 # $10 <-- 4lw $13, Buffer($10) # $13 <-- 00 00 00 02
Remember: Words are 4 bytes each!Remember: Words are 4 bytes each!
Loads from Buffer+0 Loads from Buffer+4
Ch2b- 8EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
The Assembler Location CounterThe 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: .word 12buffer2: .word 3, 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:
4004 4008
4012 4016
Hex Constants a denoted by the “0x” prefix
buffer1 = 4000
buffer2 = 4004
firstld = 4024secld = 4032
Label TableLoc. Ctr.
Instructions – stored in ordinary memory. Execute by telling the CPU to start running at location 4020.Instructions – stored in ordinary memory. Execute by telling the CPU to start running at location 4020.
Ch2b- 9EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Other Memory Assembler Directives
borg: .byte 33, 22, 12, 10, 8, 1
greeting: .asciiz “Resistance is futile.”
greeting2: .ascii “You will be assimilated.”
69 73 65 52greeting:
0 2E 65 6C
...
Null-terminatedNull-terminated
20 75 6F 59
-- 2E 64 65
...greeting2:
.byte - reserves bytes in memory
.asciiz - reserves Null-terminated ASCII chars
.ascii - reserves ASCII characters (no NULL)
10 12 22 33borg:? ? 1 8
Resi
le.
Ch2b- 10EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Meeting all your needs for space
Sometimes, we need to allocate (empty) space to be used later.
inputbuffer: .space 100
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, 6sw $12, inputbuffer($0) # stores 6 in buffer
.align 2inputbuffer: .space 100
If the space is to be used as words (with LW,SW), make sure it is aligned to a multiple of 22 using the .align directive
Ch2b- 11EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Our first program!# This is our first program! Yeah!
.data
Tonto: .word 0x44, 0x22
.text
main:add $9, $0, $0 # clear $9lw $8, Tonto($9) # put Tonto[0] in $8addi $9, $9, 4 # increment $9lw $10, Tonto($9) # put Tonto[1] in $10
addi $v0,$0,10syscall
.data means that data follows .data means that data follows
.text means that code follows.text means that code follows
main: tells SPIM where to startmain: tells SPIM where to start
these two instructions end the programthese two instructions end the program
Ch2b- 12EE/CS/CPE 3760 - Computer OrganizationSeattle 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 PCSPIMat 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
Ch2b- 13EE/CS/CPE 3760 - Computer OrganizationSeattle 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, $3 add $5, $3, $0
neg $8, $9 sub $8, $0, $9
li $8, 44 addi $8, $0, 44 or ori $8, $0, 44
Ch2b- 14EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Pseudoinstructions for branches• Branches can be nasty to figure out
• SPIM provides several pseudoinstructions for branches
blt $3, $4, dest slt $1, $3, $4bne $1, $0, dest
bgt $3, $4, dest slt $1, $4, $3bne $1, $0, dest
$3 > $4 same as$4 < $3$3 > $4 same as$4 < $3
ble $3, $4, dest slt $1, $4, $3beq $1, $0, dest
bge $3, $4, dest slt $1, $3, $4beq $1, $0, dest
$3 >= $4 is the opposite of $3 < $4$3 >= $4 is the opposite of $3 < $4
$3 <= $4 is the opposite of $3 > $4$3 <= $4 is the opposite of $3 > $4
Ch2b- 15EE/CS/CPE 3760 - Computer OrganizationSeattle 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 stringprompt: .asciiz “hello world” la $a0,prompt li $v0, 4 syscall
Action Code (in $v0) ParametersPrint an Integer 1 $a0 = value to printPrint a String 4 $a0 = location of stringInput an Integer 5 (after syscall) $v0 contains integerInput a String 8 $a0 = location of buffer, $a1 = lengthExit program 10
Ch2b- 16EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Data Structures - ArraysA single-dimensional array (vector) is a simple linear data structure
20002004200820122016202020242028
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 computationsneeded to access any other elements.
Ch2b- 17EE/CS/CPE 3760 - Computer OrganizationSeattle Pacific University
Accessing data structures in memoryAssume that the variable List points to the beginning of an arrayof 32-bit integers.
List=6000 123600032886004
43600816012
456016...
...
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 <-- 4lw $4, List($8) # $4 <-- List[1]
addi $8, $0, 16 # $8 <-- 16lw $5, List($8) # $5 <-- List[4]
Move List[4] into $5:
List and contents of $8 are added together to form addressList and contents of $8 are added together to form address
List[0]List[1]List[2]List[3]List[4]