0% found this document useful (0 votes)
66 views23 pages

Chapter 6 Introduction To MIPS Assembly Language

The document provides an overview of MIPS assembly language programming, including the architecture, instruction set, and the use of registers. It discusses the importance of learning assembly language for understanding computer operations and design, and outlines the structure of simple programs in MIPS. Key components such as special-purpose registers, instruction formats, and basic operations are also highlighted.

Uploaded by

hung.lq05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views23 pages

Chapter 6 Introduction To MIPS Assembly Language

The document provides an overview of MIPS assembly language programming, including the architecture, instruction set, and the use of registers. It discusses the importance of learning assembly language for understanding computer operations and design, and outlines the structure of simple programs in MIPS. Key components such as special-purpose registers, instruction formats, and basic operations are also highlighted.

Uploaded by

hung.lq05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

10/28/2024

Assembly Language
Programming
Dr Truong Dinh Huy

Previous Weeks

• Boolean Algebra and Logic gates

• Digital Circuit and Design

• Combinational Circuits: Decoder, Adder,…

• Sequential Circuits: Flip-Flop

1
10/28/2024

Outline

• MIPS assembly language.

• 32-bit Registers of MIPS processor.

• QtSPIM/MARS software.

• Register-Register instructions.

Assembly Language

• Basic job of a CPU: execute lots of instructions.

• Instructions are the primitive operations that the CPU


may execute.

• Different CPUs implement different sets of instructions.


The set of instructions a particular CPU implements is
an Instruction Set Architecture (ISA).
• Examples: Intel 80x86 (Pentium 4), IBM/Motorola
PowerPC (Macintosh), MIPS, APPLE/M1, ...

2
10/28/2024

Instruction Set Architectures (ISA)


Early trend was to add more and more instructions to new
CPUs to do elaborate operations
VAX architecture had an instruction to multiply
polynomials!

Two basic ISAs: RICS (ARM/MIPS) and CISC (Intel x86)

RISC philosophy (Cocke IBM, Patterson, Hennessy, 1980s)


– Reduced Instruction Set Computing
• Keep the instruction set small and simple, makes it
easier to build fast hardware.
• Let software do complicated operations by composing
simpler ones.

MIPS Architecture
MIPS – semiconductor company that built one of the
first commercial RISC architectures.

We will study the MIPS ISA in some details in this


class.

Why MIPS instead of Intel 80x86?


• MIPS is simple, elegant. Don’t want to get
bogged down in gritty details.
• MIPS widely used in embedded devices.

3
10/28/2024

Why Learn Assembly Language?


• Compilers greatly ease the program development load. Assemblers are
much more primitive. Then why learn assembly language?
• First, compilers remove the visibility of computer operation.
• Second, using assembly language gives a better feel for computer operation.
• Third, learning assembly language aids in understanding computer design.
• Fourth, assembly language improves precision (and reduces size) of
programs.
Write program in
compiler language We will skip this step.

Resulting assembly language program


Compiler (or write program in assembly language) We will do
these steps
Assembler “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

The MIPS assembly language and


SPIM
• We will study the assembly language of the MIPS processor.

• The architecture was very influential – many electronic game


systems today employ a descendant of the original MIPS.

• We will study the first MIPS model, the 32-bit MIPS R2000.

• The assembler/simulator for the MIPS R2000 is


• QtSPIM
• MARS

• MIPS reference data card.


8

4
10/28/2024

Instruction Processing of MIPS


Processor
• In all computers, an instruction is retrieved from memory, the fields of
the instruction are decoded, and the decoded commands are processed.

Register divided

Instruction
into fields of bits
Decoders
Register

Result (to
Registers or
Registers

Decoders
Memory Memory)
Register
Instruction

Block ALU
Instr.

Decoders

Registers
• There are 32 registers in the MIPS processor.

• Remember that registers are just collections of D flip-flops.

• The number of register bits (width) depends on the processor.

• The MIPS R-2000 computer has 32-bit data words, so each register
has 32 flip-flops.

• The MIPS computer has 32 fixed-point (whole number) registers.

• There are rules (actually suggestions or policies) for register use.

• The rules may be violated; the assembler has no “Register Police.”

19 • However, to do so is very bad practice.

5
10/28/2024

MIPS Special-Purpose Registers


• These registers are generally used only for the purposes noted:
Registers Name or Value Purpose
$0 Always = 0 Comparisons; clearing registers
$1 $at Assembler temporary register1
$2 $v0 Value to be passed to function2
$3 $v1 Value to be passed to function
$4 $a0 Argument passing to procedures3
$5 $a1 Argument passing to procedures4,5
$6 $a2 Argument passing to procedures4
$7 $a3 Argument passing to procedures4
In MIPS, the dollar sign (“$”) signifies “register.”
1 – Don’t ever use this register. 2 – Used in all system calls to load the syscall number; also
specifically used in syscalls 5 & 12 for input data. 3 – Specifically used in syscalls 1, 4, 8, and
11. 4 – Argument passing past a total of four uses the stack. 5 – Also used in syscall 8.

20

Special-Purpose Registers (2)

• These registers are also generally used only for the purposes noted.

Register Name Purpose


$26 $k0 Reserved for use by operating
system.1
$27 $k1 Reserved for use by operating
system.1
$28 $gp Pointer to global area2
$29 $sp Points to current top of stack
$30 $fp Points to current top of frame
$31 $ra Return address to exit procedure
1 – Don’t ever use these registers! 2 – We will not use this register.

21

6
10/28/2024

MIPS Temporary Registers

• The t-registers hold data in programs. Use as you wish.


Register Name Purpose
$8 $t0 Holds a local (temp) variable
$9 $t1 Holds a local (temp) variable
$10 $t2 Holds a local (temp) variable
$11 $t3 Holds a local (temp) variable
$12 $t4 Holds a local (temp) variable
$13 $t5 Holds a local (temp) variable
$14 $t6 Holds a local (temp) variable
$15 $t7 Holds a local (temp) variable
$24 $t8 Holds a local (temp) variable
$25 $t9 Holds a local (temp) variable
• NOTE: Think of t-registers as “scratchpad registers.”
22

MIPS Saved Temporary Registers

• S-registers also hold data in programs. In general, use as you wish.


Some restrictions on s registers will be discussed later.

Register Name Purpose


$16 $s0 Holds a saved temporary variable
$17 $s1 Holds a saved temporary variable
$18 $s2 Holds a saved temporary variable
$19 $s3 Holds a saved temporary variable
$20 $s4 Holds a saved temporary variable
$21 $s5 Holds a saved temporary variable
$22 $s6 Holds a saved temporary variable
$23 $s7 Holds a saved temporary variable

23

7
10/28/2024

A Few Things We Need to Know

• To write a program in QtSPIM, we need to know how to


start, end and format the instructions of a program.
• We’ll learn more about directives in the next lecture. All
you need to know today is that all programs are started
with the directive .text.
• Beneath that directive, start writing the program
instructions. ONLY 1 INSTRUCTION PER LINE.
• Labels are important in QtSPIM. ANY instruction may
be labeled or not as you choose, except for the 1st
instruction in the program. It MUST be labeled main:

15

A Few Things We Need to Know (2)

• The program must be in one contiguous string of


instructions (1 per line).
• The .text directive ALWAYS starts the program.
• At the end of the program, you must use syscall 10 to
end the program. It says “halt!”
• There are a number of system calls that allow the user
to communicate with the program. They all have the
same form. Load register $v0 with the number of the
syscall, then execute the syscall instruction.
• Thus: li $v0,10 stops the program.
syscall
16

8
10/28/2024

A Few Things We Need to Know (3)

• The MIPS R-2000 has a lot of registers to store


information. Many of them are available to the user.
• We will learn more about MIPS registers in a few
moments. Right now, here are the identities of a few of
the registers you can use in a program (note that the $
sign is the QtSPIM designation meaning “register”:
—$t0 —$t3
—$t1 —$t4
—$t2 —$v0 (for syscalls)

17

Structure of a Simple Program


• Suppose we want to calculate 6482 + 13292 =?
• How might we do that in a program?
• First, remember that ALL data used in calculations MUST
be in one of the CPU registers.
• Thus we must load the registers with the numbers to operate
on them.
• We can puts any number up to 16 bits in the designated register
by using “addi” instruction:
• addi $t0, $0, 648 #puts that decimal number 648 in $t0.
• addi $t1, $0, 1329.
• add $t2, $t1, $t0.
18

9
10/28/2024

Structure of a Simple Program (2)

• “.text” = “Program follows”


• Program starting instruction
always labeled “main:” .text
• Label always ends with colon main: addi $t0, $0, 648
(:) . addi $t1, $0,1329
• One instruction per line. mul $t2,$t0,$t0
• Syscall 10 ends the program. mul $t3,$t1,$t1
• All program instructions add $t4,$t2,$t3
must be grouped together
under the .text label. li $v0,10
• Check $t4 in the register syscall
window for the answer.
19

3 basic forms of MIPS instruction


• R-form (Register-Register Instructions)
• add, sub, mult, …, and, or, sll, …
• I-form (Memory-Register Instructions)
• addi, andi, lw, sw, lb, sb, lui, …
• J-form (Jump Instructions)
• j, jal
• The program is written in assembly language, a
sequence of acronyms which represent instructions:
– The assembler converts assembly language instructions into
patterns of binary numbers that make up the 32-bit instruction.
– Registers ID’s are converted to binary numbers that are also
inserted into appropriate fields in the 32-bit instruction.
– The final 32-bit instruction consists of fields of binary numbers,
each of which represents part of the instruction.
7 • We don’t deal with floating point instructions in this course

10
10/28/2024

SPIM Register-Register Instructions


• Today, we will study just a few instructions that will allow us
to write a simple program.
• “Register-to-register” instructions do all the calculations
and data manipulations.
• "add” instructions are the R-form:
• Operation_Dest. Reg., Source Reg., 2nd Source Reg.
For example: add $t2,$t0,$t1

Op Code Rs Rt Rd Sh Amt Fn Code


000000 01000 01001 01010 00000 100000
Reg./Reg. $t0 $t1 $t2 Shft.=0 add
Computer instruction as loaded: 00000001000010010101000000100000
01095020 (hexa)

Assembler Process Diagram


Instruction: add $t2,$t0,$t1 # $t2= $t0 + $t1
(R-form)

Begin “add” instruction ID first source reg.


process. and input 5-bit #
into “Rs” field.
ID the arithmetic/
Insert “000000” in bits
logic function and ID destination reg. ID second source reg.
26-31, since all
insert 6-bit binary # and input 5-bit # and input 5-bit # into “Rt”
arithmetic instructions
into “function code” into “Rd” field. field.
have a “0” op code.
field.
Bit 0
Op Code Rs Rt Rd Sh Amt Fn Code

Bit 31
Instruction fields The function code is the arithmetic/
op: operation code (opcode) (6 bits) logical function to be performed.
Example: Function code 10 0000 [0x20]
Rs: first source register number (5 bits) means “add.”
Rt: second source register number (5 bits)
Rd: destination register number (5 bits)
22
Sh Amt: shift amount (00000 for now) (5 bits)
Fun Code: function code (extends opcode)(6 bits)

11
10/28/2024

Register-Register Instruction Process

Register Block

ALU processes instruction


$t0
ALU “add”

$t1

$t2 Note: You can add (or subtract, or divide, or perform


any math operation) on only 2 numbers at a time!
23

R-R Instruction Example


op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

add $t0, $s1, $s2

special $s1 $s2 $t0 0 add

0 17 18 8 0 32

000000 10001 10010 01000 00000 100000

000000100011001001000000001000002 = 0232402016

12
10/28/2024

Register-Register Instructions: addi


Bit 31 Rs Rt Bit 0

6 bits 5 bits 5 bits 16 bits

• “addi” instruction is I-format.

• In addi, the second source is an “immediate,” i.e., a


number coded in the instruction (16 bits). 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


instructs the computer to ignore overflow indication.
• Note: add (and ALL MIPS instructions) can process
only two (2) numbers.
24

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 there is not a formal “subi,” a coded number in


the instruction can be substituted for rt. Thus:
– sub $t3, $t4, 67: [$t4] ‒67 → [$t3].
– sub $s3, $s6, 0xdf8: [$s6] ‒0xdf8 → [$s3].

Sub with immediate value is a pseudo instruction

• Like “addu,” subu ignores an overflow indication

25

13
10/28/2024

Example
C code:

f = (g + h) - (i + j);
Assume that g in $t0, h in $t1, i in $t2, j in $t3, f in $t4. Compiled
MIPS code:

add $t5,$t0,$t1 # temp t5 = g + h


add $t6,$t2,$t3 # temp t6 = i + j
sub $t4,$t5,$t6 # f = t5 – t6

25

Multiply
• Multiply is written:
mult $t0, $t1  {$Hi, $Lo} = $t0*$t1

• Since multiplying two 32-bit numbers can have a 64-bit result, the
MIPS computer provides two 32-bit registers for a multiply result,
LO and HI .
• Multiply sends the upper 32 bits to HI and the lower 32 bits to LO.
• Note: You must check HI for a residual result (product > 32 bits).
SPIM does NOT do this!
• If we only care about the lower 32 bits of result:
mul $t2, $t0, $t1  $t2=$t0*$t1

SPIM transfers the 32-bit result from LO to the destination register.

26 Notes: The second operand for mul instruction can be a constant, as for addi.

14
10/28/2024

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

• Another MIPS divide: div $t0, $t2, $t1


– div $t2,$t1; mflo $t0 (where mflo means [LO]→[$t0]).
– That is, div $t0, $t2, $t1 means: [$t2]/[$t1] → [LO] → [$t0]

• SPIM recognizes div $t2,$t1, which results in the


quotient remaining in LO and the remainder in HI.

• div $t0, $t2, $t1 is a Pseudo instruction.


* The second operand (divisor) can be a number, as for mul, add, sub, etc.

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]= 0001110….0111

[$t2]= 0001110….1100

[$t0]= 0001110….0100

• Thus in the above example, bit 1 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 0 of $t2, and the result stored in bit 0 of $t0, etc.

27

15
10/28/2024

Instructions for bitwise manipulation

Operation C Java MIPS


Shift left << << sll
Shift right >> >>> srl
Bitwise AND & & and, andi
Bitwise OR | | or, ori
Bitwise NOT ~ ~ nor

 Useful for extracting and inserting


groups of bits in a word

Shift Operations
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits

shamt: how many positions to shift


Shift left logical
Shift left and fill with 0 bits
sll by i bits multiplies by 2i
Example:
If $t2 = 0000….0000101:
sll $t3, $t2, 3 => $t3 = 0000…..0101000
Shift right logical
Shift right and fill with 0 bits
srl by i bits divides by 2i (unsigned only)

16
10/28/2024

AND Operations
Useful to mask bits in a word
Select some bits, clear others to 0

and $t0, $t1, $t2

$t2 0000 0000 0000 0000 0000 1101 1100 0000

$t1 0000 0000 0000 0000 0011 1100 0000 0000

$t0 0000 0000 0000 0000 0000 1100 0000 0000

We also have “andi”

OR Operations
Useful to include bits in a word
Set some bits to 1, leave others unchanged

or $t0, $t1, $t2

$t2 0000 0000 0000 0000 0000 1101 1100 0000

$t1 0000 0000 0000 0000 0011 1100 0000 0000

$t0 0000 0000 0000 0000 0011 1101 1100 0000

We also have “ori”

17
10/28/2024

NOT Operations

Useful to invert bits in a word


Change 0 to 1, and 1 to 0
MIPS has NOR 3-operand instruction
a NOR b == NOT ( a OR b ) Register 0: always
read as zero
nor $t0, $t1, $zero

$t1 0000 0000 0000 0000 0011 1100 0000 0000

$t0 1111 1111 1111 1111 1100 0011 1111 1111

Pseudo Instructions

• Pseudo instructions are instructions that exist in the


SPIM assembler, but are not instructions designed into
the MIPS computer. Most pseudo instructions are of
the register-to-register type.

• Pseudo instructions make SPIM a bit more compiler-


like. They are typically made up of two or more MIPS
machine instructions, although a few are single
instructions.

• We will see a several pseudo instructions today.

18
10/28/2024

Other Pseudo Instructions


• lui $rd, #: “load upper immediate.” The upper 16 bits of $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  (0000 0000 0001 0111 0000 0000 0000 0000) → $t1.

• li $rd, #: “load immediate.” Although this is a register-type instruction,


li is a pseudo instruction which combines ori and lui to create the
desired load. Example:
– li $t0, 2405 (# < 16 bits): ori $t0, $0, (binary number [less than 16 bits])
– li $t0, 78645329 (# > 16 bits): lui $at, [upper 16 bits of binary number]
ori $t0, $at, [lower 16 bits of binary number]

“li” instructions may change the value of $at silently, that is why we don’t use
$at in our assembly program.

Other Pseudo Instructions


• move: move transfers the contents of one register to
another. Example:
– move $s5, $t3: [$t3] → [$s5] ($t3 contents are not changed).
– move is also a pseudo instruction, using: addu $s5,$0,$t3.

• The “neg” instruction simply changes the mathematical


sign of the number in the source register. Thus:
– neg $t2, $s5: ([$s5]+1) → [$t2]. (Do you recognize this formula?)
• “abs” is also a pseudo instruction. It takes the absolute
value of the source register.
– If the source is positive, the number → destination “as is.”
– If the source is negative, the two’s complement → destination.
– The destination always ends up with a positive number equaling
the magnitude of the number in the source.
29
– Example – abs $t1, $t0: |[$t0]| → [$t1].

19
10/28/2024

Let’s Do a First Program


• To write a simply program, remember:
• Write your program in Notepad and store in the SPIM folder.
– Each program must start with the “.text” directive on the first line
(without the quotes).
– On the second line, put the first instruction. Note: It must be labeled
“main:” in the margin (again, no quotes).
– Remember: the instruction “li” puts a number in any register, e.g.,
li $t1,273. Remember to put a space after the “li.”
– A few of the registers we can use are $t0, $t1, $t2, $t3.
– And remember that:
li $v0,10
syscall
tells your program to “stop!” Use them last.
• See your program outline to check your formats.

16

Let’s Do a First Program (2)


• Here’s what your program should do:
– Put 10 in $t0 and 5 in $t1 (using the “li” instruction).
– Add the contents of $t0 and $t1 and put it in $t2 (use “add”).
– Add the contents of $t0 and $t2 and put it in $t3.
– Stop the program.
– The “add” instruction looks like: add $rs, $rt, $rd -- $rs is the
first source register, $rt is the second source, and $rd is the
destination. Note that either number can be in either register.
• After the program runs, look at the register display
(left side panel of MIPS display). Find $t3.
• Contents of $t3 should be 0x19. When you see this
number in $t3, you have completed your first program.

17

20
10/28/2024

Program 2
• Write a program on your computer to do the
following:
– Enter 47 into $t0 (“x”)
– Put 253 into $t1 (“y”)
– Load 23 in $t2 (“z”)
– Compute x2 + 3y + 10z
– Store the result in $t3
– Stop the program by using system call 10.
– What is the number in $t3?

35

Program 3

• Write a program to do the following:


– Load 25 in $t4 and 126 into $t7.
– Add the contents of registers $t4 and $t7, storing in
$t3.
– AND this result with $t4, storing in $s2.
– Multiply $s2 by the contents of $t7, storing in $t9.
Also store the result in $t9 in $s5 and $s7.
– Use syscall 10 to end the program.
– What is the value of the number in $s7 (state in
hex)?

37

21
10/28/2024

Program 4
• Write a program to solve the equation: w = 4x2+8y+z.
• Variable x is 0x123, y is 0x12b7, and z is 0x34af7.
• Use $t0 for x, $t1 for y, and $t2 for z.
• Remember to start your program with .text and label the
first instruction as “main:”.
• Store w in $s0 and end your program with syscall 10.
What is the value of w in hex?
• Try this program on your own

39

Summary

• Introduction to MIPS assembly language

• Some basic instructions for integers:


• add (addi, addiu) and sub
• mul (mult) and div
• logical instructions
• lui, li
• move,
• …

22
10/28/2024

Next week

• Learn more about directive of MIPS

• Instructions about load and store data

23

You might also like