Thursday, 21 January 2010

Moving to another blog

I haven't posted anything on here for a long time now and that was mainly because I was busy and I started moving the blog to another place. I am going to be moving the blog because wordpress is more convenient for me.

I moved most of the contents there and filled in some of the gaps in updates using notes that I have kept during the term on my progress but I am still organizing some of my notes so it well take a bit of time before everything is up there.

so the new blog is here.

Wednesday, 21 October 2009

Week 3: MIPS Assembly Language

This Week, we were given a task to learn and do a simple exercise with an assembler language called MIPS. So as useal with some other stuff I'm gonna drop off some notes. I am gonna explain small details to get the grasp of them but it is not very detailed so look up for more detailed description of each stuff listed below.

When using MIPS (or most of assembly languages) you have something called register which is basically a temporary storage. Registers used in MIPS are 32bit long meaning that there are 32 different registers to be used (from range $0 to $31). Some of the registers are reserved for particular purposes e.g. register $0 contains 0 and it cannot be changed.

Basic Operations:

addu operation adds values from two register and stores it to one e.g.
addu $7, $7, $13 #ax + b
- addu in the front describes the operation.
- First $7 indicates the destination of the result.
- Second and thired register shows which registers to add the values together
- '#' is used for comments, anything after # will not be executed.


addi operation adds a value in register with a constant value e.g.
addi $7, $7, 1 #a++ ($7 = $7 + 1)
- same as addu, addi shows the operation, first $7 shows the destination,
- The second register shows which register to add the constant to
- The third one is the constant to add to the register
Note that there is a operation called add but it doesn't deal with overflow so that program gets trapped (stops working and return the control to the operating system, basically a crush)


ori operation stores a value into a register e.g.
ori $8, $0, 4 #initialize $8
This line basically stores 4 into register $8.


subu operation is basically same as addu but subtract instead of adding (a bit obvious I guess)
subu $10, $11, $10 #subtract ($10 = $11 - $10)
It basically calculates $11 - $10 and store it in $10.


There is not subi in MIPS so instead you have to add an negative number to a register e.g.
addiu $8, $8, -552 #add -552 to $8
so result is (value in $8 + (-552))


To carry out multiplication, you can use sll operation e.g.
sll $10, $9, 2 #multiply $9 by 4
The sll operation (Shift Left Logical) multiply $9 by 2 which means you are shifting the bits to left by 2 bits. Shifting one bit is same as multiplying by 2 and the amount goes up in power of 2 i.e. in this example, it shifts 2bits so its 2^2 = 4. If the constant was 4 then it would be 16. If you need to multiply a value by odd number e.g. 5, you can shift bits by 2 and add one of the same value e.g.
sll $10, $9, 2 #multiply $9 by 4
addi $10, $10, 2 #$9 x 4 + 2 = $9 x 5
There is also a srl (Shift Right Logical) which is the same but in opposite direction i.e. it divides the value.


There is another operation for multiplying in MISP which is multu. When you use mult to calculate, the calculation is carried out in two 32bit registers (called hi and lo). Those registers are specific ones which you can not use in normal calculations so there is the mlhi and mflo operators to move them to general purpose registers. mlhi move the value from 'hi' register and mflo move value from the 'lo' register e.g.
mult $11, $11 #x squared
mflo $11 #assume 32-bit result and move to $11
mult multiply the two registers specified.


There is a instruction to load a word (4byte) to a register, the lw instruction. But there is a problem if you just try and load it into register straight way because a MISP instruction is 32bit long and a MIPS register in 32 bit long so it won't work out. Any instruction that uses the memory use base register and offset. The base register contains a 32bit address and the offset is a 16bit signed integer stored in the instruction.


You can use lui instruction which I will not explain deeply here but it sets up which base register to load the word to and stores it into a register. e.g.
lui $10, 0x1001 #initialize base register
lw $11, 0($10) #load x from $10 int0 $11
lw $12, 4($10) #load a
lw $13, 8($10) #load b
...more instructions...
.data
x: .word 1
a: .word -3
b: .word 3

- lui $10 shows which register to store it to
- The 0x1001 part of the instruction indicates which base register to load.
- Going back to lw, lw $11 bit specifies which register to store to.
- 0($10) shows where to load the word from.
- 0 in front of the 0($10) shows add 0 to location of $10.
- Second line of lw specifies different register to store to (I guess this is obvious)
- The second lw has 4($10) instead of 0($10) because 0x1001 ($10) has already being used so you want to specify different location.
- The .data indicates that all the code from here on is a for data
- "x:" is the lable for this data, .word specifies its data type and 1 is its value.


This post became much longer than I thought it was going to be and the last bit got very close to just being a quote of a website. So for more details, look in the reference.
Reference: http://chortle.ccsu.edu/AssemblyTutorial/index.html