You are on page 1of 27

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Assembly Language Programming


First computer programs: Rewiring! (Remember the Lecture 1 picture?) p ) Early programmers literally wired the computer circuits to solve the desired problem. Later stored-program computers, were programmed by loading memory (electrical relays, magnetically-coated rotating g drum) ) with instructions manually, y, one at a time. There was a great need to make programming easier. The first programming support programs were assemblers.
1 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Assembly Language Programming (2)


Assemblers allow the programmer to use simple names for instructions in writing a program.
Add, for example. The program is constructed using instruction acronyms. The assembler converted the English text names to the combination of 1s and 0s that represented each instruction in the computer. Automatic program loading was developed (e.g.: tape, disk).

Later, compilers arrived on the scene.


Provide high level of abstraction (takes care of details). One compiler statement = many machine instructions.
2 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Why Learn Assembly Language?


Compilers high level of abstraction eases program development p load, , but reduces the feel of the softwarehardware interaction. That is, compilers remove the lower-level visibility of computer t and d program operation ti (an ( example l shortly). h tl ) Assembly language programming gives students a better feel for computer p operation. p Learning and using assembly language also helps in understanding much of the basics of computer design. Some programs still need assembly language precision.
3 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Steps in Constructing a Program


Write program in compiler language Compile Resulting assembly language program (or write program in assembly language) Assemble Object code, or machine language module, with relative memory references Link or link edit Object code or machine language with library functions or macros Load Machine instructions loaded in computer; absolute memory references 4 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

We will not do this step in EE 2310

We will do these four steps in EE 2310

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Assembler Code is Precise


Suppose a program required the swap of two data locations in memory. We might write a C routine such as this:
swap( int v[] , int k ) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; }

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

Lecture #10: Assembly Language Programming; Introduction to SPIM

N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll


000000 27bdfff0 000004 afa40010 000008 afa50014 # Source offset: 40 00000c 8fa80014 000010 00000000 000014 00084880 000018 8faa0010 00001c 00000000 000020 01495821 000024 8d6c0000 000028 00000000 00002c afac000c # Source offset: 57 000030 8fad0014 000034 00000000 000038 21ae0001 00003c 000e7880 000040 8fb80010 000044 00000000 000048 030fc821 00004c 8f280000 000050 8fa90014 000054 00000000 000058 00095080 00005c 8fab0010 000060 00000000 000064 016a6021 000068 ad880000 # Source offset: 76 00006c 8fad000c 000070 8fae0014 000074 00000000 000078 21cf0001 00007c 000fc080 000080 8fb90010 000084 00000000 000088 03384021 00008c ad0d0000 # Source offset: 91 000090 27bd0010 000094 03e00008 000098 00000000 addiu $sp,$sp,-16 sw $a0,16($sp) sw $a1,20($sp) lw $t0,20($sp) nop sll $t1,$t0,2 lw $t2,16($sp) nop addu $t3,$t2,$t1 lw $t4,0($t3) nop $t4,12($sp) , ($ p) sw $ lw $t5,20($sp) nop addi $t6,$t5,1 sll $t7,$t6,2 lw $t8,16($sp) nop addu $t9,$t8,$t7 lw $t0,0($t9) lw $t1,20($sp) nop sll $t2,$t1,2 lw $t3,16($sp) nop addu $t4,$t3,$t2 sw $t0,0($t4) lw $t5,12($sp) lw $t6,20($sp) nop addi $t7,$t6,1 sll $t8,$t7,2 lw $t9,16($sp) nop addu $t0,$t9,$t8 sw $t5,0($t0) addiu ddi $ $sp,$sp,16 $ 16 jr $ra nop

Erik Jonsson School of Engineering g g and Computer Science


# push arguments, etc. onto the stack # store 1st argument as addr v[0] # store 2nd argument as k # load k into register $t0 # put 4*k in register $t1 # load address of v[0] into $t2 # addr v[k] = addr v[0] + 4*k # load v[k] into $t4 # store v[k] [ ] into temp p # load k into register $t5 # put k+1 into register $t6 # multiply k+1 by 4; result in $t7 # load address of v[0] into $t8 # addr v[k+1] = addr v[0] + 4*(k+1) # load v[k+1] into $t0 #load k into register $t1 # multiply k by 4; put result in $t2 # load address of v[0] into $t3 # addr v[k] = addr v[0] + 4*k # store $t0 (i.e., v[k+1]) into v[k] # load temp (i.e., v[k]) into $t5 # load k into $t6 # put k+1 into $t7 # put 4*(k+1) into $t8 # load address of v[0] into $t9 # addr v[k+1] = addr v[0] + 4*(k+1) # store $t5 (i.e., old v[k]) to v[k+1] # pop arguments, t etc. t off ff th the stack t k # return to the calling routine

Swap Program Assembly Code Derived from Compiler

Lecture #10: Assembly Language Programming; Introduction to SPIM

N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

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

end of file Swap


Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Summary: Why Program in Assembly Language?


Get a good view of how the target processor really works works. Get a better understanding of basic software concepts as well:
Use of a stack How a loop operates Why procedure calls and other reusable code need to be carefully written How the computer functions as it executes a program

Get a feel for how to write an efficient program!


8 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

The MIPS Computer and SPIM


The MIPS computer, whose architecture we will study near the end of this course, was developed by Patterson and Hennessey, who wrote one of our textbooks. textbooks There have been a number of MIPS models; the original design, which dates from the 1980s, has been superseded. Th basic The b i architecture hit t h has b been very i influential, fl ti l h however many electronic game systems in use today employ a descendant of the original MIPS computer. W will We ill study t d one of f th the fi first t MIPS models, d l th the MIPS R R-2000. 2000 While this is only a 32-bit computer, its design and system details are sufficiently complex to make it a challenging choice. Th assembler/simulator The bl / i l t for f the th MIPS R R-2000 2000 i is called ll d SPIM SPIM.
Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Segments of a SPIM Program


The parts of a SPIM program are generally as follows:
Comments to introduce, comment on, or end the program. The text section (actual program instructions). Either text or data may come first. The data section (identifying data elements).

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Typical SPIM Program Outline


Comments: Title, any special notes on the program, explanation of instructions or instruction groups. Comments always y defined/set off by yp pound sign g (#) only y 1/line. Text (the actual program):
Started with the .text directive. One SPIM instruction p per line; can comment on the instruction line. Each instruction must include operands and destination of result. Any text line may contain a label (but not required). The only required label is main: on the first line of text. A label must be followed by a colon (:). (:)

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

Lecture #10: Assembly Language Programming; Introduction to SPIM

N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Instruction Execution on a MIPS Computer


Most computers (including MIPS) execute instructions in the method shown below:
Obtain instruction and information to process (usually from data registers). Interpret instruction and do processing in CPU arithmetic unit (ALU or datapath). Put results in storage (in data registers).

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

SPIM Register-Register g g Instructions


We will study other instruction types next lecture. Today, we begin the processing instructions, which will give us an easy entry into a little practice programming. programming These instructions are called register-to-register instructions because in general, data to be processed is obtained from a register and the result of the ALU processing is returned for register, storage to another (or even the same) register. There are 32 registers in the MIPS computer. They will be discussed and identified in the next lecture lecture. SPIM register-to-register instructions are usually of the form:
Operation_Dest. Reg.,Source Reg.,2nd Source Reg.


14

Register i instructions i i do all the data processing i in i SPIM. S


Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Multiply as a Pseudo Instruction


Multiply sends the upper 32-bit result to HI and the lower 32 bits to LO.
Th The assembler bl accomplishes li h thi this b by adding ddi a second d instruction: mul $t1, $t2; mflo $t0. (mflo means [LO] [$t0]) SPIM recognizes mul $t1,$t2, with results left in LO and HI.
HI (upper 32 MIPS instruction: mul $t1,$t2 means [$t1][$t2] bits) In SPIM, mul $t0, $t1, $t2 implies [$t1][$t2][$t0]. LO (lower 32 bits)

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Divide as a Pseudo Instruction


Similarly, the MIPS divide is:
div $t2,$t1, which means [$t2]/[$t1] LO (fixed point 32 bit quotient)
HI (32 bit remainder)

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Logical Instructions (2)


AND function:
and $s0, $t3, $t4: [$t3][$t4] [$s0], on a bitwise basis.

OR is performed likewise:
or $s0, $t3, $t4: [$t3]+[$t4] [$s0], on a bitwise basis.

andi/ori These instructions are similar to addi, thus:


ori $t4, $t4 $t5, $t5 0x0ff1: [$t5] + 0xff1 [$t4], [$t4] on a bitwise basis basis. andi $t4, $t5, 587: [$t5 ] 587 [$t4], on a bitwise basis.

NOR and XOR are functionally identical to AND and OR.


nor $t1, $t1 $t2 $t2, $t6 $t6: [$t2] + [$t6] [$t1], [$t1] on a bit bitwise i basis. b i xor $s1, $s2, $s3: [$s2] [$s3] [$s1], on a bitwise basis.

NOT is a simpler instruction, with only one operand:


not $t7, $s5: [$s5] [$t7], on a bitwise basis.
Lecture #10: Assembly Language Programming; Introduction to SPIM

21

N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Neg and Abs


The neg instruction simply changes the mathematical sign of the number in the source register. g Thus:
neg $t2, $s5: ([$s5]+1) [$t2].

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

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Other Register-Register Instructions


lui $rd, #: load upper immediate. The upper 16 bits $rd are loaded with the immediate (#). The lower half is cleared to 0. Since lui is not a pseudo instruction, it is limited to a 16 bit immediate (convenient, since there are 16 bits in the upper half of a register). Example:
lui $t1, 23 The number 23 (0000 0000 0001 0111) upper 16 bits of $t1.

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

Lecture #10: Assembly Language Programming; Introduction to SPIM

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

Other Register-Register Instructions (2)


move: move is also a pseudo instruction. In move, the contents of one register are transferred to another. Example:
move $s5, $t3: [$t3] [$s5] ($t3 contents are not changed).

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

Th U The University i it of f Texas T at t Dallas D ll

Erik Jonsson School of Engineering g g and Computer Science

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

Th U The University i it of f Texas T at t Dallas D ll

Erik Jonsson School of Engineering g g and Computer Science

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

Lecture #10: Assembly Language Programming; Introduction to SPIM

N. B. Dodge 09/09

Th U The University i it of fT Texas at tD Dallas ll

Erik Jonsson School of Engineering g g and Computer Science

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.

Read Pervin, chapter 3. Read Patterson and Hennessy, 2.1-2.2.


27 Lecture #10: Assembly Language Programming; Introduction to SPIM
N. B. Dodge 09/09

You might also like