Lecture6-MIPSAssemblyII - Tonga Institute of Higher Education

Download Report

Transcript Lecture6-MIPSAssemblyII - Tonga Institute of Higher Education

Tonga Institute of Higher Education
IT253: Computer Organization
Lecture 6:
Assembly Language
and MIPS:
Programming
Assembly and Machine
Language
• Machine language is a sequence of binary numbers
that can be translated into instructions
• Assembly language is the text representation of
machine language
• A compiler will take high level code, change it to
assembly (low level code), which can then be
converted directly into executable code
The MIPS Assembly Language
• MIPS Languages details:
– One instruction per line.
– Numbers are base-10 integers or Hex.
– Variables: alphanumeric (can use numbers and integers
for names)
– Labels: identifiers starting at the beginning of a line
followed by “:”
– Comments: everything following # till end-of-line.
– Instruction format: Space and “,” separated fields
• Example: add $s1,$s2,$s3 # comment (R-type)
<op> reg1, reg2, reg3 # comment
Directives in MIPS
• Directives tell the assembler what to do.
• They are not actual assembly, but help
programmers write assembly code
– Format “.”<string> [arg1], [arg2] . . .
– Examples
• .align 2 # align data on every word
• .data # start a data segment.
• .text # start a code segment.
• .ascii <string> # store a string in memory.
• .asciiz <string> # store a null terminated string in
memory
• .word w1, w2, . . . , wn # store n words in memory
Starting a program
• To begin your MIPS program you always
have a few directives at the top:
–
–
–
–
.align 2 # tells assembler how to store data
.text # says the assembly comes next
main: # label for the start of your code
.data # put at the bottom, defines different data
Let’s look at a simple program
Simple program to add two numbers together
Let's try another program
int main()
{
int sum = 0;
for (int k = 0; k <= 100; k++) {
sum = sum + k*k;
}
cout << "the answer is " << sum << endl;
}
And Now the Assembly Program
.text # declare text segment
.align 2 # align it on 4 -byte boundary
main:
li $3,0
li $4,0
li $5,101
# $3 = 0 (k = 0)
# $4 = 0 (sum = 0)
# R5 = 101;
loop1:
beq $3,$5,end
multu $3,$3
mflo $6
addu $4,$4,$6
addi $3,$3,1
j loop1
end:
li $v0,1
move $a0,$4
syscall
jr $31
#
#
#
#
#
if (k == 101) go to end
k*k, result saved in HI and LO
move result from LO to $6
sum = sum + k*k
k++
#
#
#
#
# Now we do output…
prepare system call for print
move sum into $a0 to be printed
print sum
return to previous
Loops
• Loops do not have special commands in MIPS. You
need to just use a branch and a jump statement
together in order to achieve the same affect
• For example, in a for loop there are 3 parts
– Initialization, Checking, and Post-Condition
• The initialization should be done before the loop
starts
• The check needs to be done at the beginning of
every loop
• The post-condition should be done at the end of
every loop, but before you jump back to the start of
the loop
Loop Example
int k = 0;
while (k < 10) {
// do something
k++;
}
Condition
li $t0,0 # k = 0
li $t1,10 # check 10
while:
beq $t0,$t1,end
# do something
addi $t0,$t0,1
j while
end:
Post-Condition
Initialization
MIPS registers and naming
• MIPS registers are able to be addressed by their
number ($1, $7) or by their name ($s1, $t2)
• The names allow for programmers to remember what
the registers are traditionally used for.
System Calls
• All programming languages need a way to input and output
data. MIPS is no different in that it can:
– Read variables from input (like cin)
– Output data to the screen (like cout)
• In order for MIPS to perform these operations it requires more
steps that illustrate how the operating system handles input and
output
– Load a system call code into register $v0 (this tells the OS
what I/O you want to do)
• Example: li $v0,1
– Load arguments into registers $a0,$a1 (this passes the data
to the OS for outputting)
– Do the syscall (system call), which makes the OS perform
the operation
– Results are returned in $v0 or $a0 (if the operation got data
from the user, the data is in $v0 or $a0
MIPS system call codes
Sample program
Sample Program (continued)
Arrays in MIPS
• Arrays in MIPS are easy compared to C++.
• What you need to remember is that an array
is a block of memory
• In order to make an array of different values,
use MIPS code like this:
.data
x: .word 5,10,15,20
• This creates a label "x" that points to the
place in memory that holds the beginning of
four words (32 bits) of data in a row.
Using Arrays in MIPS
• In order to use an array, you must increment
the address of the register that stores the
address of the array.
main:
la $t0,x
lw $t1,0($t0)
addi $t0,$t0,4
lw $t2,0($t0)
#
#
#
#
put address of x[0] into $t0
put $t1 = x[0]
change address to be x[1]
put $t2 = x[1]
Functions and Recursion in MIPS
• Using functions or methods, especially with
recursion, in MIPS requires extra work.
• A function call needs to use space (in the stack) in
the memory in order to save information.
• This means it will use a place in memory to save
important data like:
– Values for arguments to the functions
– Registers that don’t get modified in the function call
– Local variables created within the function
A look at the stack during MIPS
$ra
$s0
$a0
12 bytes
Function 1
12 bytes
Function 2
Each function can take
space on the stack
The space depends on how
much data you want to save.
The stack will grow down as
you put more data in it
The stack pointer will change as each new function is called
What are the customs for
function calls in MIPS?
Steps for the code that is calling a function
• Step 1: Pass the arguments:
– The first four arguments (arg0-arg3) are passed in
registers $a0-$a3
– Remaining arguments are pushed onto the stack (in
reverse order arg5 is at the top of the stack).
• Step 2: Save caller-saved registers
– Save registers $s0-$s7 if they contain values you
need to remember
• Step-3: Execute a jal instruction.
– Jump and Link will save your return address in the
$ra register, so that you can return to where you
started
What are the customs for
function calls in MIPS?
Steps for the function that is being called
Step 1: Establish stack frame.
– Subtract the size of the data from the stack pointer.
• subiu $sp, $sp, <size>
– Typically, minimum size is 32 bytes (8 words).
• Step 2: Save callee saved registers in the frame.
– Register $ra is saved if function uses another
function.
– Registers $s0-$s7 are saved if they are used.
– Registers $a0-$a3 may need to be saved
What are the customs for
function calls in MIPS?
Steps for the code after the function is done
• Step-1: Put returned values in registers $v0, [$v1].
– (if values are returned from the function)
• Step-2: Restore callee-saved registers.
– Restore saved registers. [$ra, $s0 - $s7] that were placed in
the stack to the registers where they belong
• Step-3: Pop the stack
– Add the size of memory taken back to the $sp.
addiu $sp, $sp, <size>
• Step-4: Return
– Jump to the address in $ra.
jr $ra
Sample Program: Factorial
Sample Program
Sample program with function
• Look at the file “hanoi.s” on the website
• Hanoi is a classic computer science
program that uses recursion to solve a
problem.
• We can see in the program how the
stack is moved farther down each time,
so that we save local variables and the
return address.
Sample program using recursion
• The full text of the program is on the web. Here is the
recursive part of Hanoi
factorial:
subu $sp, $sp, 8
sw $ra, 4($sp)
sw $a0, 0($sp)
bgtz $a0, fact1
li $v0, 1
# STACK PUSH
# save return address
# save register
# branch if $a0 > 0
# return terminal value in
# register $v0
lw $ra,4($sp)
# put the return address back
lw $a0,0($sp)
# put $a0 back
addu $sp, $sp, 8 # STACK POP
jr $ra
# return to starting point
fact1:
addi $a0, $a0, -1 # num = num -1
jal factorial
# factorial ( num - 1 )
Intel 80x86 ISA
• MIPS is the instruction set for some computers and
devices
• Pentium processors and other Intel processors use
the 80x86 instruction set for computers that can run
Windows
• It is more complicated than MIPS and a little harder
to understand.
• It is only a 2 address ISA
– (MIPS is 3 address, in Intel the operand is source
and destination, like addu $t0,$t1)
• No load and store. An operand can be in memory, but
does not need to be in a register
Summary
• We covered everything from opcodes to
writing an assembly program.
• We went over how use registers, instructions
and how to save things in memory so that we
can have functions
• Also we went over system calls so we can do
input and output