Professional Documents
Culture Documents
Memory CPU
Register Block
RX
A temporary variable (temp) is created for the swap. The compiled C program is shown on the following page.
Ry
N. B. Dodge 09/09
N. B. Dodge 09/09
The swap program written in SPIM assembly language is much more efficient:
Swap Program: Swaps locations of two data words in memory. .text main: lw $t1, data1 # load first data word into register $t1 lw $t2, data2 # load second data word into register $t2 sw $t1, data2 # store first data word in second data word location sw $t2 $t2, data1 # store second data word in first data word location li $v0,10 syscall # end program .data data1: .word 0x12345 data2: .word 0x54321 #
7
SPIM
The SPIM assembler/simulator human-readable MIPS assembly language instructions.
R Runs on various i target t t CPUs, CPU since i we do d not t have h actual t l MIPS computers to operate. Comments here refer to PCSPIM, the assembler/simulator that runs on the th Wi Wintel t l PC PC. SPIM is i also l available il bl for f Mac M and d Unix. U i
SPIM uses labels and pseudo instructions to simplify programming p g g (i.e., it is very y slightly g y compiler-like). p
A SPIM instruction usually translates to 1 machine instruction, but sometimes to 2 or 3. This introduces a bit of abstraction between programmer and hardware, but not to the level of a compiler.
10 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
All data must be labeled (given an identifying acronym for the program [text] to refer to). Text statements (i.e., program instructions) may also be labeled for identification and reference in the program. Text and data directives must precede the text and data sections. sections We will discuss other directives later. later
11 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
Data:
Starts with the .data directive. Data declarations covered later. Each data statement must include a definition and a label. Data may be listed before or after text.
12
N. B. Dodge 09/09
32 Registers
Register Bl k Block
Memory y
Data Instructions
13 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
A ALU
Data
Data
14
Add
The format of add is: add rd, rs, rt. The contents of registers rs and rt (the source registers) are added, and the result is put in rd. rd Examples:
add $s1, $t2, $t6: [$t2] + [$t6] [$s1]. add $t7, $t3, $s4: [$t3] + [$s4] [$t7]. [ ] = Contents of
I In addi, ddi the th second d source i is an i immediate, di t i i.e., a number coded in the instruction.* Examples:
addi $s1, $t2, 27: [$t2] + 27 [$s1]. addi $t7, $t3, 0x15: [$t3] + 0x15 [$t7].
Adding the u at the end (i.e., addu or addiu) simply p to ignore g overflow indication. instructs the computer
* Immediates may be any size up to 32-bits (that is ~ 2,000,000,000).
15 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
Subtract
Subtract has exactly the same form as add: sub rd, rs, rt. Examples:
sub $t3, $t4, $t5: [$t4] [$t5] [$t3]. sub $s3, $s6, $t0: [$s6] [$t0] [$s3].
Although t oug there t e e is s not ot a formal o a subi, sub , a coded number u be in the instruction can be substituted for rt. Thus:
sub $t3, $t4, 67: [$t4] 67 [$t3]. sub $s3, $s6, 0xdf8: [$s6] 0xdf8 [$s3].
Like addu, subu prevents an overflow indication from being given when the instruction is executed, if overflow occurs. occurs
16 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
Multiply/Divide
Multiply and divide are real MIPS instructions, but SPIM makes them into pseudo instructions in the way that it handles the product and quotient results. In general, when multiplying two n-bit numbers, the product can be up to 2n bits. Thus, multiplication of two 32-bit numbers could result in i a 64-bit 64 i product. MIPS S provides i for f this i with i a special, i 2 X 32-bit product register, LO for the lower 32 bits, and HI for the high 32 bits (see Pervin Chapter 2, Section 2.1). Si il l we remember Similarly, b that h we are using i fixed-point fi d i numbers b without a decimal fraction, so that when dividing, the quotient (to the nearest whole number) goes in LO, and the remainder is stored in HI HI.
Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
17
Note: You must check HI for a residual result (product > 32 bits). SPIM does NOT do this!
Notes: 1. 1 [ ] = contents contents of; of; 2. 2 SPIM recognizes mul $t1, $t1 $t2, $t2 and will leave results in LO and HI; 3. The second operand for multiply can be a constant, as for add, sub, and, or, etc. 18 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
SPIM transfers the quotient from LO to the destination register, as it does the lower 32 bits in multiply. Thus the SPIM instruction div $t0, $t0 $t2, $t2 $t1 becomes: becomes:*
div $t2,$t1; mflo $t0, where mflo means [LO][$t0].
SPIM recognizes div $t2,$t1, which results in the quotient remaining in LO and the remainder in HI. In divide instructions, the 1st source is divided by the 2nd.
That is, is div di $t0, $t0 $t2 $t2, $t1 means: [$t2]/[$t1] [LO] [$t0] [$t0].
19 * The second operand (divisor) can be a number, as for mul, add, sub, etc. Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
Logical Instructions
Logical instructions perform logic functions on the arguments on which they operate. For example:
and $t0, $t1, $t2: [$t1] [$t2] [$t0].
Logical operations include AND, OR, NOT, NOR, and XOR. Logical instructions are performed on the arguments on a bitwise basis, as shown below (and $t0,$t1,$t2).
[$t1] [$t2] [$t0]
3130 2928 3130 2928 3130 2928 2 1 0 2 1 0 2 1 0
Thus in the above example, bit 0 of register t1 is ANDed with bit 0 of register t2 and the result stored in bit 0 of $t0; bit 1 of $t1 is ANDed with bit 1 of $t2, and the result stored in bit 1 of $t0, etc.
Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
20
OR is performed likewise:
or $s0, $t3, $t4: [$t3]+[$t4] [$s0], on a bitwise basis.
21
N. B. Dodge 09/09
abs is also a pseudo instruction. It takes the absolute value l of f the th source register. it
If the source is positive, the number destination as is. If the source is negative, the twos complement destination. The destination always ends up with a positive number equaling the magnitude of the number in the source. Example p abs $ $t1, ,$ $t0: |[$t0]| |[$ ]| [$ $t1]. ]
22 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
li $rd, #: load immediate. Although this is a register-type instruction, li is a pseudo instruction (Pervin, Appendix D) which combines ori and lui to create the desired load. Example:
li $t0, 2405 (# < 16 bits): ori $t1, $at, (binary number [less than 16 bits]) li $t1, $t1 78645329 (# < 16 bits): lui $at, [upper pp 16 bits of binary y number] ori $t1, $at, [lower 16 bits of binary number]
N. B. Dodge 09/09
23
rem: Same as divide, , except p that [ [HI] ] are moved to the destination register. Dangerous to use, since the sign of the numbers involved can cause the answer to be undefined. Example of use:
rem $t0, $t1, $t2 [$t1]/[$t2], remainder [HI] [$t0]. This instruction is the complement of div, since in MIPS it , $t2; ; mfhi $t0. And y yes, , the q quotient of the becomes: div $t1, divide LO, but is not used.
24 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
Exercise 1
Now for a little practice in Reg.Reg. instruction programming. We have not studied how to start a program, but for the moment, just assume that you are to write down the basic i t ti instructions ONLY. ONLY $ $ $ Write a list of instructions to add the contents of registers t1 and t2, storing in t3. AND this result with t4, storing in s2. Multiply s2 by the contents of s7, storing in t9. Also store the Remember: in SPIM programming, $ = register register. result lt i in t9 i in s5 5 and d s7. 7
Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09
25
Exercise 2
You need to solve the equation: w = 4x3+8y2+z. Variable x is in $t6, y is in $t7, and z is in $t8. Write the instruction list (ONLY) to compute the value of w below. Store the resulting value (w) in $s0.
26
N. B. Dodge 09/09
Homework
As usual:
W Write it down d th t the two or th three most ti important t t thi things you learned today and add to your list. Write down two or three things you did not clearly understand. After finishing the assigned reading, if you still have questions, see me during office hours.