MIPS - 访问指令地址以获取操作码

时间:2016-02-26 04:37:47

标签: mips

我正在编写一个程序,我循环遍历代码并计算I,J和R类型指令的数量。我将如何访问指令的操作码?

1 个答案:

答案 0 :(得分:0)

.data
typeR: .word 0x00 #for all TypeR opc code
typeJ: .word 0x01 #we will have to left shit one bit when comparing
typeI: .word 0x11 #this is a dummy value because if it's not R or J, it is I

numR: .word 0
numJ: .word 0
numI: .word 0

endProgram: .word 0x11111111 # you would have to know the address of the #last instruction. Assume for now

.text
main:
lw $s0,  countR
lw $s1,  countJ
lw $s2,  countI

#here you load the address of the first instruction of the program you try
#to loop though say 0x00000000
la $t0, 0x00000000
#here load the instruction code in 0x11111111 to register $t1
lw $t1, endProgram

Loop:

lw $s3, 0($t0) # load first instruction code to $s3

beq $s3, typeR, R #if equal goes to R
sll $s3, $s3, 1 # so we get rid of the first digit
beq $s3, typeJ, J  # if equal got to J
J I # this is the else

R:
addi $s0,$s0, 1 #increment countR
j next
J:
addi $s1, $s1, 1#increment countJ
j next
I:
addi $s2, $s2, 1 # increment countI
j next

next:#check if it is the end yet, if not keep going to next instruction
lw $s5, 0($t1)
beq $s5, $t1, exit # if it is last instructin go to exit

#if not keep going
addi $t1, $t1, 4 #move to next instruction since each is 4 byte
j loop
exit:
# I think the last part is pretty simple, depend on what you want to do just #print it out or something, so I've save some typing.