Converting a 16 Bit decimal into a 2-bit with two's complement in MIPS Assembly Language - binary

Excuse me, I am trying to type in the number such as a negative number like this : -123
Here is what I have typed in so far:
.data
putin: .asciiz "Enter the 16 bit decimal number: "
decimalbit: .asciiz "\nThe Decimal is: "
binarybit: .asciiz "\nThe Binary is: "
.text
main:
li $v0, 4
la $a0, putin
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0, 4
la $a0, decimalbit
syscall
li $v0, 1
move $a0, $t0
syscall
Can some give me pointers on how I should do it?
Thanks

div stores the remainder in $HI. The result can be used with mfhi.

Related

How do I do 2's compliment in MIPS?

How do I do 2's compliment in MIPS? I need to print the decimal, binary, and hex with 2's compliment. This is what I have so far:
.data
int: .word 0
prompt: .asciiz "Please enter a 16 - bit integer: "
msgDec: .asciiz "\nYour number as a decimal: "
msgBin: .asciiz "\nYour number in binary: "
msgHex: .asciiz "\nYour number in Hexadecimal: "
twoComp: .asciiz "\nYour number in decimal with 2's comp: "
twoCompBin: .asciiz "\nYour number in binary with 2's comp: "
twoCompHex: .asciiz "/n Your number in Hex with 2's comp is: "
.text
# Display first prompt
li $v0, 4
la $a0, prompt
syscall
#user input
li $v0, 5
syscall
#print as decimal
move $t0, $v0
li $v0, 4
la $a0, msgDec
syscall
li $v0, 1
move $a0, $t0
syscall
#print as Binary
li $v0, 4
la $a0, msgBin
syscall
move $a0, $t0
li $v0,35
syscall
move $t1, $a0 #move binary into $t1
#print as hex
li $v0, 4
la $a0, msgHex
syscall
move $a0, $t0
li $v0,34
syscall
move $t2, $a0 #move hex into $t2
#2s compliment
li $v0, 4
la $a0, twoComp
syscall

How do I get my code for addition of two numbers to work in QtSpim?

I'm new to MIPS and I've written a program to add two user-selected numbers but it's not working. It says Instruction references undefined symbol at 0x00400014. Can someone please go through my code and explain what's wrong? Thank you.
.data
msg1: .asciiz "Enter addend 1: "
msg2: .asciiz "Enter addend 2: "
msg3: .asciiz "Sum = "
.text
li $v0, 4
la $a0, msg1
syscall
li $v0, 5
syscall
move $t0, $v0
li $v0, 4
la $a0, msg2
syscall
li $v0, 5
syscall
move $t1, $v0
add $t2, $t0, $t1
li $v0, 4
la $a0, msg3
syscall
li $v0, 1
move $a0, $t2
syscall
li $v0, 10
syscall

How do you add in mips?

First i know ive asked tons of questions but ive come a long way since i last asked.
Im using mars simulator for some side project learning.
Ive always been interested in assembly and mips is my choice.
Anyways my program seems to crash after i try and move my values and add them up and i cant figure out why.
Thanks for looking and tips will be great.
Have a good day
.data
msg0: .asciiz "enter a number: \n"
msg1: .asciiz "enter another number: \n"
result: .asciiz "result is: \n"
.text
li $v0, 4
la $a0, msg0 #load first message
syscall
li $v0 5
move $t0, $v0 #user input
syscall #store number
li $v0, 4
la $a0, msg1 #second msg
syscall
li $v0, 5
move $t1, $v0 #second input and store number
syscall
li $v0, 4
la $a0, result #rsult message
syscall
add $t3,$t1,$t0 #and my problem is here for some reason?
syscall
You need to add a label that are function names. In the code I have provided below, the function name is main. Also, the last system call serves no purpose now. What you need to do is indicate that your code has finished before making the last system call by li $v0,10.
.data
msg0: .asciiz "enter a number: \n"
msg1: .asciiz "enter another number: \n"
result: .asciiz "result is: \n"
.text
.globl main
main:
li $v0, 4
la $a0, msg0 #load first message
syscall
li $v0 5
syscall
move $t0, $v0 #first input and store number
li $v0, 4
la $a0, msg1 #second msg
syscall
li $v0, 5
syscall
move $t1, $v0 #second input and store number
li $v0, 4
la $a0, result #result message
syscall
add $t3,$t1,$t0
move $a0, $t3
li $v0, 1
syscall #print answer
li $v0,10
syscall

MIPS : Printing n time a number in a file

I'm currently working on a project for school. I'm asked to generate a random list of number in a text file, and first of all I tried to print n times a number in a test file, 31 for example. However, no matter the value of n, I always get my test.txt file with "31 " printed in but only one time. Here's my code (I'm a beginner btw) :
.data
question: .asciiz "\nEnter a number : ?\n"
mynumber: .asciiz "31 "
file: .asciiz "test.txt"
.text
.globl __start
__start:
li $v0, 4
la $a0, question
syscall
li $v0, 5
syscall
move $t0 $v0
li $t1 0
Loop:
blt $t0 $t1 exit
addi $t1 $t1 1
jal open_file
jal fill_file
jal close_file
j Loop
open_file:
li $v0, 13
la $a0, file
li $a1, 1
li $a2, 0
syscall
jr $ra
fill_file:
move $a0, $v0
li $v0, 15
la $a1, mynumber
li $a2, 3
syscall
jr $ra
close_file:
li $v0, 16
syscall
jr $ra
exit:
li $v0, 10
Does somebody have a solution for my problem? Would appreciate ! (Sorry for my english, not a native speaker)

accessing first character of an string and comparing it with an char MIPS

I want to compare a first character of a string with '#' char. If these are equal I want to print "they're equal" in mips. To do this, I've written a piece of code as below.However it does not give me an output even if they're equal. Is there anyone to help me ?
Thanks in advance.
.data
input: .space 201
string2: .asciiz "they're equal.\n"
finish: .byte '#'
.text
main:
la $a0,input
li $a1,201 #read 200 char
li $v0,8 #read string
syscall
jal evaluate
evaluate:
lw $t1, 0($a0)
lw $t2,finish
beq $t1,$t2,testi
testi:
la $a0,string2
li $v0,4
syscall
li $v0, 10
syscall
Yes, you have placed the branch in such a way that the next instruction is the same regardless of whether or not the branch is taken.
Consider changing it to something like this:
evaluate:
lw $t1, 0($a0)
lw $t2,finish
bne $t1,$t2,testi
la $a0,string2
li $v0,4
syscall
test1:
li $v0, 10
syscall