在MIPS中打印字符串数组

时间:2015-11-03 02:54:44

标签: arrays mips

我正在处理一个应该接受来自用户的输入(数字1-9)的程序,然后使用该输入来打印已完成的MIPS指令(1 = add,2 = addi ,3 =或等)。

这样的一个例子是用户输入数字1,程序输出" add $t2, $t0, $t1"。用户输入数字2,程序输出" addi $t3, $t2, 100" (寄存器是连续的和链接的。)

因此,如上所述,程序的一个功能是在用户输入数字时迭代寄存器$t0-$t9。我认为打印这些寄存器及其相关操作码的最佳方法是将它们放入一个数组中并循环遍历它们。但是,我无法找出MIPS是否/如何打印出字符串数组(是的,我看到另一个stackoverflow帖子提出同样的问题,但我不太明白答案,因为它'用C)写的。

我真的很感激人们能给予的任何帮助!

这是我的代码的相关部分(我知道它并不多,对不起):

.data
registers: .asciiz "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8", "$t9"  #this is the array that I want to print
registerSize: .word 9

.text 
readOpcodes:    #read the user-entered ints (1-9) in preparation for numberDecisions
add $t0, $t0, $zero
lb $s0, opcodes($t0)

numberDecisions:    #decide which instruction to output based on user input
beq $s0, 1, addOpcode 
beq $s0, 2, addiOpcode 
beq $s0, 3, orOpcode 
beq $s0, 4, oriOpcode
beq $s0, 5, lwOpcode
beq $s0, 6, swOpcode
beq $s0, 7, jOpcode
beq $s0, 8, beqOpcode
beq $s0, 9, bneOpcode 

registerAllocation: #this is all I've really got when it comes to printing out the register array
addi $s1, $zero, 0  #register counter = 0
la $s2, registers   #load register array in $s2
la $s3, registerSize    #load register array size into $s3

addOpcode:
li $v0, 4   #print 'add' opcode
la $a0, addPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes    #Ideally, I'd be able to print the registers (in the proper order depending on the opcode, obviously) in this section for each opcode

addiOpcode:  #feel free to ignore the rest; it's just the process of printing all 9 opcodes. Sorry if it's not completely relevant, but I wasn't sure how clear my code would be if I left it all out
li $v0, 4   #print 'addi' opcode
la $a0, addiPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes

orOpcode:
li $v0, 4   #print 'or' opcode
la $a0, orPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes

oriOpcode:
li $v0, 4   #print 'ori' opcode
la $a0, oriPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes

lwOpcode:
li $v0, 4   #print 'lw' opcode
la $a0, lwPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes 

swOpcode:
li $v0, 4   #print 'sw' opcode
la $a0, swPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes

jOpcode:
li $v0, 4   #print 'j' opcode
la $a0, jPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes

beqOpcode:
li $v0, 4   #print 'beq' opcode
la $a0, beqPrint
syscall

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes

bneOpcode:
li $v0, 4   #print 'bne' opcode
la $a0, bnePrint
syscall 

addi $t0, $t0, 1
beq $t0, 5, end
j readOpcodes

end: 
li $v0, 10
syscall

0 个答案:

没有答案