参考リンク
- Computer Organization and Design MIPS Edition, Fifth Edition: The Hardware/Software Interface (The Morgan Kaufmann Series in Computer Architecture and Design)
- MIPS Reference Data Card 上の本の付録
- qtspim .. http://spimsimulator.sourceforge.net/
- qtspim tutorial .. https://ecs-network.serv.pacific.edu/ecpe-170/tutorials/qtspim-tutorial
instruction
- jal .. The instruction actually saves
PC+4
in register $ra to link to the following instruction to set up the procedure return (ascall
in x86)
jr $ra
.. Same as ret
in x86
mnemonic
Mnemonic | Meaning | Type | Opcode | Funct |
---|---|---|---|---|
add | Add | R | 0x00 | 0x20 |
addi | Add immediate | I | 0x08 | NA |
addiu | Addi unsigned | I | 0x09 | NA |
addu | Add unsigned | R | 0x00 | 0x21 |
and | Bitwise AND | R | 0x00 | 0x24 |
andi | Bitwise AND Immediate | I | 0x0C | NA |
beq | Branch if Equal | I | 0x04 | NA |
blez | Branch if Less Than or Equal to Zero | I | 0x06 | NA |
bne | Branch if Not Equal | I | 0x05 | NA |
div | Divide | R | 0x00 | 0x1A |
divu | Unsigned Divide | R | 0x00 | 0x1B |
j | Jump to Address | J | 0x02 | NA |
jal | Jump and Link | J | 0x03 | NA |
jr | Jump to Address in Register | R | 0x00 | 0x08 |
lbu | Load Byte Unsigned | I | 0x24 | NA |
lhu | Load Halfword Unsigned | I | 0x25 | NA |
lui | Load Upper Immediate | I | 0x0F | NA |
lw | Load Word | I | 0x23 | NA |
mfhi | Move from HI Register | R | 0x00 | 0x10 |
mflo | Move from LO Register | R | 0x00 | 0x12 |
mfc0 | Move from Coprocessor 0 | R | 0x10 | NA |
mult | Multiply | R | 0x00 | 0x18 |
multu | Unsigned Multiply | R | 0x00 | 0x19 |
nor | Bitwise NOR (NOT-OR) | R | 0x00 | 0x27 |
xor | Bitwise XOR (Exclusive-OR) | R | 0x00 | 0x26 |
or | Bitwise OR | R | 0x00 | 0x25 |
ori | Bitwise OR Immediate | I | 0x0D | NA |
sb | Store Byte | I | 0x28 | NA |
sh | Store Halfword | I | 0x29 | NA |
slt | Set to 1 if Less Than | R | 0x00 | 0x2A |
slti | Set to 1 if Less Than Immediate | I | 0x0A | NA |
sltiu | Set to 1 if Less Than Unsigned Immediate | I | 0x0B | NA |
sltu | Set to 1 if Less Than Unsigned | R | 0x00 | 0x2B |
sll | Logical Shift Left | R | 0x00 | 0x00 |
srl | Logical Shift Right (0-extended) | R | 0x00 | 0x02 |
sra | Arithmetic Shift Right (sign-extended) | R | 0x00 | 0x03 |
sub | Subtract | R | 0x00 | 0x22 |
subu | Unsigned Subtract | R | 0x00 | 0x23 |
sw | Store Word | I | 0x2B | NA |
https://inst.eecs.berkeley.edu/~cs61c/resources/MIPS_help.html もいい感じにまとまっている
example
- I-type
instruction | instr | opcode | rs | rt | immediate | remarks |
---|---|---|---|---|---|---|
bits | 31:0 | 31:26 | 25:21 | 20:16 | 15:0 | |
addi $s0, $0, 5 |
addi $rt, $rs, imm |
0x08 | 0x00($0) | 0x10 | 0x05 | |
lw $s0, 100($1) |
lw $rt, imm($rs) |
0x23 | 0x11 | 0x10 | 0x64 | |
sw $s0, 100($1) |
sw $rt, imm($rs) |
0x2B | 0x11 | 0x10 | 0x64 | lwと逆の操作だが、rs, rtは同じ |
- R-type
|instruction|instr|opcode|rs|rt|rd|shamt|funct|remarks|
|---|---|---|---|---|---|---|---|---|---|
|bits|31:0|31:26|25:21|20:16|15:11|10:6|5:0||
|sll $t2, $s0, 4
|sll $rd, $rt, shamt
|0x00|0x00|0x10|0x0a|0x04|0x00|
|and $t2, $s0, $s1
|and $rd, $rs, $rt
|0x00|0x10|0x11|0x0a|0x00|0x24|
addressing
registers
-
$at .. assembler temporary. This is used to store the immediate number that is parted by the 32-Bit Immediate Operands like
lui
(load upper immediate). -
PC(Program counter): Instruction address register. The register holds the address of the current instruction being executed.
-
$k0-$k1
Reserved for OS Kernel
syscall
spim はあくまでmipsシミュレータなので、本格的なオペレーティングシステムはもっていないが、入出力ができないと不便なので、簡単なOS的な機能を持っている。それを呼び出すと入出力ができる。
exec
memory layout
Fig2.13 The MIPS memory allocation for program and data.
R29 [sp] = 7fffeffc
とあるので、stack領域-0x1000
で0x1000[byte]
分なんかに使っているみたい(環境変数が入ってる??)
main
mainのentrypointは0x00400024
でそれまで何をしてるか。
# Register number
## $v0-$v1 .. 2-3, $a0-$a3 .. 4-7, $sp .. 29, $ra .. 31
User Text Segment [00400000]..[00440000]
## argc
[00400000] 8fa40000 lw $4, 0($29) ; 183: lw $a0 0($sp)
## argv
[00400004] 27a50004 addiu $5, $29, 4 ; 184: addiu $a1 $sp 4
## envp
[00400008] 24a60004 addiu $6, $5, 4 ; 185: addiu $a2 $a1 4
# Shift left by constant
[0040000c] 00041080 sll $2, $4, 2 ; 186: sll $v0 $a0 2
[00400010] 00c23021 addu $6, $6, $2 ; 187: addu $a2 $a2 $v0
# For procedure call(jump and link)
[00400014] 0c100009 jal 0x00400024 [main] ; 188: jal main
[00400018] 00000000 nop ; 189: nop
# no longer execute
## syscall 10 (exit)
[0040001c] 3402000a ori $2, $0, 10 ; 191: li $v0 10
[00400020] 0000000c syscall ; 192: syscall
# この後、mainのentrypoint