Introduction to MIPS Assembly Language
Review of Previous Lecture Bitwise Logical Operations
OR, AND, XOR: One register and immediate value or another register
Copyright By Assignmentchef assignmentchef
Shift operations: logical and arithmetic shift
As programmers, what functions can be implemented by these operations: multiple, clear, move, load,
Review of Previous Lecture Arithmetic Operations
Add, sub, mul, div
Dealing with positive and negative numbers
(unsigned/2s complement): add and mul are different Memory Access
Base address + offset Symbolic address
When Learning Instructions
Know what operations the AUL will do
As programmers, we can interpret the operations in different ways to implement certain functions (however, with restrictions)
Example: Bitwise OR Load (just one way)
Todays Lecture Control Program Flow
Jump and conditional branch instructions
How to implement high-level language structures, e.g.,
if else, loop,
Subroutine
How to use subroutines to write more complex programs
Program Flow Control
Program is the ordered sequence of instructions
instructions are executed sequentially
Text section in Main Memory
Instruction 4 Instruction 3
Instruction 2 Instruction 1
Address Low to High
Program Flow Control
Program is the ordered sequence of instructions
instructions are executed sequentially
we want more complex structures: e.g., in high-level-
languages, we have if-else, for loop, while ( condition),
assembly language does not have such instruction; instead, we can build up these structures from basic instructions
How Does the Jump Instruction Work? Recall: j addr
Recall: instruction cycle
fetch update PC execute
how j addr should work?
fetch j addr updata PC execute j addr (PC = addr)
NOT that easy!
Branch Delay Slot
Underlying reason (not required): MIPS is pipelined
to increase speed, processor will cleverly fetch multiple instructions and starts working on them all a pipeline of instructions
the instruction following jump is almost completed when jump is executed
instead of wasting efforts, processor will allow to complete that instruction after jump instruction
the instruction right after jump instruction is called Branch Delay Slot
Branch Delay Slot
Effect: branch delay slot will always be executed
how to deal with this? put a no-op instruction right after jump instruction
example:
Importantly, PC will be set to addr after the branch delay slot is executed (not after j addr is executed)
Example of Jump
PC should have been set as 0x00400000 after jump is executed, if there is no pipeline mechanism
Instead, PC is set to 0x00400000 after the branch delay slot is executed
Example: a simple loop
Note the instruction addiu $8, $8, 1 after j main
Question: after the loop has executed five times, whats the value in $8? (all registers are initialized to 0 when program starts)
Example: a simple loop
addiu $8, $8, 1 will always execute
Note symbolic address main
You can define a symbolic address for any block of instructions
Target: xxx xxx xxx
Other Branch Instructions
The take-away message for Jump addr
jump to the instruction specified by addr
normal practice: use symbolic address; put a no-op
instruction right after jump instruction There are other branch instructions
main study goal: how to use these instructions to implement control structure in high-level languages
e.g., jump could be used to implement unconditional loop
Conditional Branch Instructions
A conditional branch instruction branches to a new
address only if a certain condition is true
branch on equal: beq u,v, addr # if $u == $v, branches to addr
branch on not equal: bne u,v, addr # if $u != $v, branches to addr (note: compare the bit patterns in $u and $v for beq and bne)
branch on less than zero: bltz s, addr # if $s <0, branches to addr; (note: $s in 2s complement form)- branch on greater than or equal to zero: bgez s, addr # if $s>=0, branches to zero
Flow Chart of A Program
Essentially, the conditional branch instructions implement the if then do sth control structure
branch on equal: beq u,v, addr # if $u == $v, branches to addr
if $8!=$9, the block of instructions will be executed
Flow Chart of A Program
Two-way decision control structure: if true, do A; else do B
combine of conditional branch and jump
Program Example
Program task: calculate the absolute value of A stored at a given address
idea: if A < 0: store -A; else do nothing- we can use one conditional branch instructionA good practice: draw the flow chart of the program first Program Example.global main# Is A negative?# Store -A done:A: .word -1load A to register(assume A is stored at address 0x10000000; what should be blank be?)implement the conditionif no, branch to done (do nothing); else, store -A lui $10, 0x1000 # initialize the base register lw $8, [ ] ($10) # load A to register $8Program ExampleDetermine if A is negative or not- check the sign bit (at position 31) of A (assume 2s complement form)- shift right logical: srl $t0, $t1, const; shift the bits in $t1 to the right for const positions, and fill the left positions with 0; store the result in $t0- note, we stored the value of A in $8; question: whats the value of $9 after srl $9, $8, 31 Program ExampleDetermine if A is negative or not- the value of $9: first 31 bits are all 0, the last bit is the sign bit- as a result, if $9 == 0, A is positive (or 0) – exercise: fill in the blanks.globl mainlui $10, 0x1000 # initialize the base registerlw $8, 0 ($10) # load A to register $8 # Is A negative?srl $9, $8, [ ] # shift the sign bit to position 0 beq $[ ], $[ ], done # check if $9 == 0sll $0, $0, 0 # branch delay slot# Store -Adone: .dataA: .word -1 Program ExampleDetermine if A is negative or not- the value of $9: first 31 bits are all 0, the last bit is the sign bit- as a result, if $9 == 0, A is positive (or 0) – exercise: fill in the blanks.globl mainlui $10, 0x1000 # initialize the base registerlw $8, 0 ($10) # load A to register $8 # Is A negative?srl $9, $8, 31 # shift the sign bit to position 0 beq $0, $9, done # check if sign bit == 0sll $0, $0, 0 # branch delay slot# Store -Adone: .dataA: .word -1 Program ExampleThe complete program .text.globl mainlui $10, 0x1000 # initialize the base registerlw $8, 0 ($10) # load A to register $8 # Is A negative?srl $9, $8, 31 # shift the sign bit to position 0 beq $0, $9, done # check if $9 == 0sll $0, $0, 0 # branch delay slot# Store -Asubu $8, $0, $8 #calculate -Asw $8, 0($10) # store -A back to the address done: .dataA: .word -1Recall: subu $t0, $t1, $t2 Count LoopA common type of program loop execute for some fixed times, controlled by an integer (counter)Three critical parts- initialize the counter- test the counter value and end the loop on the correct value- increment the counterHow many times will it execute?Count LoopA top-driven loop in assembly language- check the condition on the top- use a combination of conditional branch, jump, and conditional set instructionsConditional Set InstructionsUsed to implement relational operations- idea: set a register to 1 or 0 to show the relation between two values (often used as a flag)- note: it will not change the program flow itself rather, it can be used as some condition to trigger the branch instructionsSet on Less Than slt- slt d, s, t # $s and $t contains signed integers in 2s complement # set $d = 1 if $s < $t; else, set $d = 0 Conditional Set Instructions Other conditional set instructions- sltu d, s, t # used with unsigned integers- slti d, s, imm # Set on Less Than Immediate# if $s < imm, set $d =1; else, set $d = 0 – sltiu d, s, imm # used with unsigned integersHint: the immediate -25 and 25 are in 2s complement form. Then, addiu will do sign extension.Slt d, s, t: set d = 1 if s
Reviews
There are no reviews yet.