MIPS - Sign Extend a 2SC 8-bit register value to 32-bit 2SC

0

I'm trying to write a program that takes two 8-bit 2SC program arguments, either as binaries (ex: 0b10000000) or hexadecimals (ex: 0x80), and then sign extends them to 32 bits.

Here are the instructions I have so far:

.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

However, this does not seem to be working as expected. With the set of program arguments: "0x80 0xFF", the syscalls at the end of the program output the following:

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

However, the correct results should be:

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

Does anybody have an idea of what is going wrong here? Many thanks in advance.

mips
twos-complement
sign-extension
asked on Stack Overflow Mar 1, 2019 by Kenny Blum • edited Mar 1, 2019 by Kenny Blum

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0