3
\$\begingroup\$

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 
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jun 22, 2017 at 8:36
\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

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.

answered Jun 22, 2017 at 8:54
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.