I try to learn assembly by writing small programs. This MIPS program is supposed to read an integer from the terminal and print it byte by byte in hexadecimal. I think it works but I didn't check everything.
.data
prompt: .asciiz "Enter an integer (positive or negative): "
s1: .asciiz "B1:
s2: .asciiz " B2:
s3: .asciiz " B3:
s4: .asciiz " B4:
MyNumber: .word 0
.text
main:
addi $v0, $zero, 4 #code 4 is to print string
la $a0, prompt #loads string into register
syscall
addi $v0, $zero, 5 #code 5 is to read an integer
syscall
sw $v0, MyNumber #stores value from $v0 to input
addi $v0, $zero, 4 #code 4 is to print string
la $a0, s1 #loads string into register
syscall
la $t1, MyNumber
lb $a0, 0($t1)
lb $t0, MyNumber
addi $v0, $zero, 34
syscall
addi $v0, $zero, 4 #code 4 is to print string
la $a0, s2 #loads string into register
syscall
lb $a0, 1($t1)
addi $v0, $zero, 34 #print in hexadecimal
syscall
addi $v0, $zero, 4 #code 4 is to print string
la $a0, s3 #loads string into register
syscall
lb $a0, 2($t1)
addi $v0, $zero, 34
syscall
addi $v0, $zero, 4 #code 4 is to print string
la $a0, s4 #loads string into register
syscall
lb $a0, 3($t1)
addi $v0, $zero, 34
syscall
1 Answer 1
Firstly, you're missing some closing quotes on the data declarations at the top of the file. And you should specify that you make use of MARS specific calls.
However, when you read an integer using the "read integer" call, you read an 8-bit integer, that is, ranging from 0 to 255.
In your program you attempt to print four bytes out of this single byte, and therefore your program will print out garbage to the console.
You should simply remove the code that prints B2, B3 and B4.