MIPS-符号将2SC的8位寄存器值扩展为32位的2SC

时间:2019-03-01 20:38:09

标签: mips twos-complement sign-extension

我正在尝试编写一个程序,该程序接受两个8位2SC程序参数,它们是二进制文件(例如:0b10000000)或十六进制(例如:0x80),然后通过符号将其扩展为32位。

这是我到目前为止的指示:

.data

output1: .asciiz "The value of $s1 is:\n"
output2: .asciiz "\nThe value of $s2 is:\n"

.text

lw $s1, ($a1)                # grab first program argument, set to $s1
lw $s2, 4($a1)               # grab second argument, set to $s2

lb $t0, 1($s1)               # store second character of $s1 in $t0
beq $t0, 'b', extends1       # if $t0 = b, branch to extends1
beq $t0, 'x', extends1       # if $t0 = x, branch to extends1

lb $t0, 1($s2)               # store second character of $s2 in $t0
beq $t0, 'b', extends2       # if $t0 = b, branch to extends2
beq $t0, 'x', extends2       # if $t0 = x, branch to extends2

extends1:
    sll $s1, $s1, 24         # logical left shift 24 bits
    sra $s1, $s1, 24         # arithmetic right shift by 24 bits

extends2:
    sll $s2, $s2, 24         # logical left shift 24 bits
    sra $s2, $s2, 24         # arithmetic right shift by 24 bits

li $v0, 4
la $a0, output1
syscall                      # print "The value of $s1 is:\n"
li $v0, 34
move $a0, $s1
syscall                      # print the contents of $s1
li $v0, 4
la $a0, output2
syscall                      # print "\nThe value of $s2 is:\n"
li $v0, 34
move $a0, $s2
syscall                      # print the contents of $s2

但是,这似乎没有按预期工作。使用程序参数集“ 0x80 0xFF”,程序末尾的系统调用将输出以下内容:

The value of $s1 is:
0xfffffff8
The value of $s2 is:
0xfffffff3

但是,正确的结果应该是:

The value of $s1 is:
0xffffff80
The value of $s2 is:
0xffffffff

有人知道这里出了什么问题吗?预先非常感谢。

0 个答案:

没有答案
相关问题