Mips Syscall 5 Invalid Integer Input Error - mips

I am new to Mips. While using syscall 5, I saw that I cannot get integers bigger than 2,147,483,647. I get the error below:
Runtime exception at 0x00400004: invalid integer input (syscall 5)
The syscall I use to get integers
li $v0, 5
syscall

As of my knowledge, MIPS does not support integers greater than 32 bit. For high-level languages, the compiler translates long long int to work using 32 bit registers in assembly language.

You need to move the value in $v0 to a registor.
You should use this:
li $v0 , 5
syscall
move $t1 , $v0

Related

How to add two variables in MIPS using addu?

I am trying to convert the following python code into an equivalent MIPS code.
X = int (input())
Y = int(input())
Print(X + Y)
Following is the MIPS code I wrote.
.text
li $v0,5
syscall
sw $v0,x
li $v0,5
syscall
sw $v0,y
addu $a0,x,y
li $v0,1
syscall
.data
x: .space 4
y: .space 4
When I assemble this code I am getting the following error for "addu $a0,x,y" line.
ERROR: "x": operand is of incorrect type
I am using MARS 4.5 Software to write these codes.
Can someone please show me my mistake and tell me how to do this? Thank you in advance.
Variables represent memory locations, therefore, the MIPS processor
can only use them in load and store instructions.
The addu instruction performs addition on the content of two
Registers and store the result in the destination register.
In the above code of mine x and y represent memory locations. Therefore, the error is, with addu instruction, I can't pass x and y as they are not registers (but memory locations). That's why I got "x": operand is of incorrect type error. So what I have to do is first store the content of x and y memory locations in two registers and then add them together using addu instruction.
The corrected code is as follows.
.text
li $v0,5
syscall
sw $v0,x
li $v0,5
syscall
sw $v0,y
lw $t1,x
lw $t2,y
addu $a0,$t1,$t2
li $v0,1
syscall
.data
x: .space 4
y: .space 4

Strange MIPS address behaviour

im a newbie in MIPS and still scratching some basic commands, currently i bumped into a very strange situation, where i have 2 identical code but one runs, and the other doesnt.
This is the code that throw an exception when running at line sw $v0, input:
.data
Text_output1: .asciiz "Input number 1: "
input: .word
.text
main:
li $v0, 4
la $a0, Text_output1
syscall
li $v0, 5
syscall
sw $v0, input
li $v0, 1
lw $a0, input
add $a0, $a0, 1
syscall
This is the normal functioning one:
# Program: Hello, World!
.data
# data declaration section; specifies values to be stored
# in memory and labels whereby the values are accessed
Greeting: .asciiz "\nghfhgfhgf\n"
Text_output1: .asciiz "Number 1 : "
input: .word
.text # Start of code section
main: # Execution begins at label "main"
li $v0, 4 #in ra number 1 :
la $a0, Text_output1
syscall
li $v0, 5
syscall
sw $v0, input
li $v0, 1
lw $a0, input
add $a0, $a0, 1
syscall
I cannot see any difference between the twos, or is there something i dont know about this language?
Btw im using MARS 4.5 with JDK 13. Thank you so much.
If you're writing any MIPS assembly, you should get the official instruction set reference (MIPS32™ Architecture For Programmers
Volume II: The MIPS32™ Instruction Set).
For the sw instruction it states the following:
Restrictions:
The effective address must be naturally-aligned. If either of the 2 least-significant bits of the address is non-zero, an
Address Error exception occurs.
As the simulator clearly states, you have an sw instruction at address 0x0040001c that tries to write to address 0x10010011, which is not a word-aligned address.
Your second example happens to work out of pure luck, because your two strings occupy 24 bytes of memory, so the input label ends up 24 bytes from the start of the .data section, which is a word-aligned address.
To ensure proper alignment, use the .align directive, e.g.:
.align 2
input: .word 0
Also note the 0 after .word. If you leave out the initial value and just write .word you will run into problems if you add more variables (they will all get the same address).

MIPS - how to store char values into space

I tried to store a char in to a space x
.data
x: .space 1
.text
.globl main
main:
lb $t0, '*'
sb $t0, x
lb $a0, x
li $v0, 11
syscall
jr $ra
it shows "Bad address in data/stack"
but it works perfectly when i use int
.data
x: .space 4
.text
.globl main
main:
li $t0, 6
sw $t0, x
lw $a0, x
li $v0, 1
syscall
jr $ra
Whats the difference between them? Why the int one works but the char one does not?
Judging by the system calls you're using, you're running this in a simulator like SPIM or MARS. The simulator showed you what the problem is:
Runtime exception at 0x00400000: address out of range 0x0000002a
And at address 0x00400000 in the code window you can see lb $8,0x0000002a($0).
So you're trying to load a byte from address 0x2a (0x2a happens to be the ASCII code for '*').
Indeed, if you look up LB in the instruction set reference from MIPS, you'll see:
LB rt, offset(base)
Description: rt ← memory[base+offset]
Obviously this is not the instruction you want for loading the constant value '*'. For that you should be using the li pseudo-instruction (or addi or ori).
TL;DR: You're tring to use a memory load instruction to load an immediate constant. Don't ignore the information that the simulator is giving you.

Mips : "Memory Address Out Of Bounds" - Error?

If I Understand well, This Program Should Print a string that entered by a User,
As It will Store its value in $a0, After that print what in $a0
But it gives me Error - "Memory Address Out Of Bounds"
Am I understand something wrong or what ??!
Thanks In Advance
.data
.text
main:
li $v0, 8
syscall
li $v0, 4
syscall
li $v0, 10
syscall
.end main
syscall with $v0 = 8 requires an address of a buffer in $a0 and the length of the passed buffer in $a1.
Check if you are passing proper arguments when you call main.
If not, allocate a buffer (with .space or something) and set the parameters for the system call properly.

How to use floating point registers in MIPS

I am trying to make a simple program for adding two floating point numbers in MIPS using SPIM simulator.The code is shown below:
.data
prompt1: .asciiz "\nPlease Enter first no then hit <enter>:"
prompt2: .asciiz "\nPlease Enter second no then hit <enter>:"
result: .asciiz "\nSum is : "
.text
main:
li $v0 , 4
la $a0, prompt1
syscall
li $v0, 5
syscall
move $f1,$v0
li $v0 , 4
la $a0, prompt2
syscall
li $v0, 5
syscall
move $fp2 , $v0
add.d $fp4,$fp2,$fp0
li $v0,4
la $a0,result
syscall
li $v0 , 1
move $a0 , $fp4
syscall
li $v0 , 10
syscall
When I load the program in the PCSPIM simulator, it gives me an error message. But when I am using general registers like $s0, $s1, $s2) instead of floating point registers to add two simple integers, it works perfectly.
How can I add two floating point numbers by using floating point registers?
Floating point registers in MIPS reside in co-processor 1 because co-processor 1 is intended to be used for FPU.
So to move data to a floating point register you will have to use mtc1 instruction instead of the move instruction. It means "move to co-processor 1".
You can refer this document online for instruction set manual:
http://www.cs.cornell.edu/courses/cs3410/2008fa/MIPS_Vol2.pdf
EDIT: Also naming of floating point registers is $f0-$f31 and not $fp0-$fp31.
The error "An error occured while loading the file" is not a runtime error, but an assembler error. In particular, your source will not assemble as it has multiple problems:
You cannot do move $f1,$v0, you have to use the mfc1 (move from coprocessor 1) and mtc1 (move to coprocessor 1) instructions for moving data from/to floating point registers.
Floating point registers aren't named fpN, but f0 to f31. Should you want to use double precision, you have to convert after loading, with (for instance) cvt.d.w $f0, $f2
If assembly were successful, add.d $fp4,$fp2,$fp0 would be trying to add the floating point registers f0 and f2, but you have loaded the supplied integers to f1 and f2.