添加位 x32 mips 组件
我正在尝试在函数中添加 2 个位串,但无法让它工作...有什么建议吗?下面是我的代码:
$a0, "11111111111100000001111111111110" #= -4064
$a1, "00000000001000111000000000000010" # = 9088
$a2, resultSpace
$a3, representation #one's or two's complement
li $t4, 0 #carry flag
lb $t0, 32($a0)
lb $t1, 32($a1)
move $t5, $a0
loop:
lb $t0, 32($a0)
lb $t1, 32($a1)
add $t3, $t0, $t1
sb $t3, 32($a2)
sub $a2, $a2, 1
sub $a0, $a0, 1
sub $a1, $a1, 1
addi $t4, $t4, 1 # increments
bne $t4, 32, loop
I am trying to add 2 strings of bits in a function and i cannot get it to work... any suggestions? here is my code below:
$a0, "11111111111100000001111111111110" #= -4064
$a1, "00000000001000111000000000000010" # = 9088
$a2, resultSpace
$a3, representation #one's or two's complement
li $t4, 0 #carry flag
lb $t0, 32($a0)
lb $t1, 32($a1)
move $t5, $a0
loop:
lb $t0, 32($a0)
lb $t1, 32($a1)
add $t3, $t0, $t1
sb $t3, 32($a2)
sub $a2, $a2, 1
sub $a0, $a0, 1
sub $a1, $a1, 1
addi $t4, $t4, 1 # increments
bne $t4, 32, loop
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一些提示。
您将位与ASCII字符“0”和“1”混淆了。 “1”是字符 49,“a”是字符 97)。
您首先查看每个字符串中的偏移量 32。但是字符串的长度是 32 个字符,因此它们的有效偏移量是 0..31,因此您要做的第一件事就是读取输入数据的末尾。
您没有在加法中使用进位标志。当您添加位时,您也没有执行任何操作来检查进位。
但是,您正在使用您声称是进位标志的寄存器作为循环计数器。
我距离 MIPS 汇编器专家还很远,所以这两个可能是错误的:
您在代码开头是否缺少一些指令?我的意思是,您真的可以只说
$a2, resultSpace
还是您需要la $a2, resultSpace
或类似的东西?是否存在 bne-immediate 伪指令?因为我很确定不存在实际的 bne-immediate 指令; MIPS 指令集中的
bne
指令期望获得两个寄存器,而不是一个寄存器和一个立即数。Here are a few pointers.
You're confusing bits with ASCII characters '0' and '1'. If you add '0' and '1' you get not '1' but 'a' (because '0' is character 48, '1' is character 49, and 'a' is character 97).
You're starting by looking at offset 32 in each string. But the strings are 32 characters long, so the valid offsets into them are 0..31, so the very first thing you do is to read off the end of the input data.
You're not using the carry flag in the addition. Neither are you doing anything to check for carries when you add bits.
You are, however, using the register that you claim is the carry flag as a loop counter.
I'm very far from being a MIPS assembler expert so these two may be wrong:
Are you missing some instructions at the start of your code? I mean, can you really say just
$a2, resultSpace
or do you needla $a2, resultSpace
or something of the kind?Is there a bne-immediate pseudoinstruction? Because I'm pretty sure there isn't an actual bne-immediate instruction; the
bne
instruction in the MIPS instruction set expects to be given two registers rather than a register and an immediate value.