280 likes | 462 Views
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
E N D
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 # $3 = 0 (k = 0) li $4,0 # $4 = 0 (sum = 0) li $5,101 # R5 = 101; loop1: beq $3,$5,end # if (k == 101) go to end multu $3,$3 # k*k, result saved in HI and LO mflo $6 # move result from LO to $6 addu $4,$4,$6 # sum = sum + k*k addi $3,$3,1 # k++ j loop1 end: # Now we do output… li $v0,1 # prepare system call for print move $a0,$4 # move sum into $a0 to be printed syscall # print sum jr $31 # 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
int k = 0; while (k < 10) { // do something k++; } li $t0,0 # k = 0 li $t1,10 # check 10 while: beq $t0,$t1,end # do something addi $t0,$t0,1 j while end: Loop Example Condition Initialization Post-Condition
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
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 # put address of x[0] into $t0 lw $t1,0($t0) # put $t1 = x[0] addi $t0,$t0,4 # change address to be x[1] lw $t2,0($t0) # 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 12 bytes $ra $s0 Function 1 $a0 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 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 # STACK PUSH sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save register bgtz $a0, fact1 # branch if $a0 > 0 li $v0, 1 # 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