MIPS?
MIPS is a 32bit RISC instruction set which has been in use since 1985 and finds a home nowadays in many simple devices, such as microwaves. It was used in the Playstation 1, 2 and Portable. Over the years, many revisions have been made, with the latest being MIPS32, and MIPS64, a 64bit version of MIPS for more modern applications.
Analysing a few lines of MIPS assembley
This piece of code was ripped from the top of the Carmageddon Rom.
0x0: ori rt 0, rs 28, 0x4012=16402
0x4: sll rd 1, rt 0, 28
0x8: mfhi rd 0
The first line is an “Or Immediate” function, which performs a bitwise or test between rs28 and “0x4012=16402” which acts like a constant, and stores the result in rt 0. I haven’t been able to find what “0x4012=16402” would refer to, but it is a 16bit value since “ori” is an I-Type function. (UPDATE: I'm amazed I didn't realise this before, but what this is actually doing is storing the hexadecimal value 0x4012 in register rt 0. The 16402 is just the value of 0x4012 in decimal) The different types are like so:
R-Type 6bit opcode, 5bit reg, 5bit reg, 5bit reg, 6bit function
I-Type 6bit opcode, 16bit Immediate Value (constant)
J-Type 6bit opcode, 26bit address
R-Type and I-Type are used for most functions, all arithmetic being R. J is used for only 2 functions, both to do with jumping to instructions.
The second line represents a bit-wise shift in the register “rd 1” of 28 which is stored in register “rt 0”. Basically this moves all the bits 1 to the left. Providing no 1’s were lost off the end in the process, this can be used to perform extremely cheap x2 calculations. For example, 0100 which is 4 in decimal would become 1000 which is 8 in decimal, hence x2. With 28 shifts it would be the same as multiplying by 2^28 (once again, providing no 1’s are lost, in which case the calculation is destroyed).
The third line indicated a “Move from HI” operation. This is a special register used by MIPS when calculating complex arithmetic. After a multiplication, a 64bit return value is stored between the 2 32bit registers, and after a division, the result is stored in LO with the remainder stored in HI. As you would imagine, there is also a “Move from LO” operation which moves a value from the LO register to another register.
Jumping
Jumping to memory address for instructions is done with the “j” function. It simply takes a memory address where the next instruction is, and goes there AFTER the next instruction is executed. For this reason, the command sll rd 0, rt 0, 0 is used. This basically performs a left bitwise shift on register 0 by 0 and storing the result in 0. This does absolutely nothing, as the 0th register in MIPS is reserved as a 32bit 0 value, so nothing can change it. The operation is referred to as a NO-OP, which literally means no operation. Now the program jumps up to the specified instruction in the “j” command.
References
http://chortle.ccsu.edu/AssemblyTutorial/index.html#part1 - Detailed information regarding MIPS assembley programming
http://en.wikipedia.org/wiki/MIPS_architecture - Overview, as well as instruction codes
http://www.zophar.net/utilities/n64dev.html - N64 / PS1 disassembler
No comments:
Post a Comment