How do I do 2's compliment in MIPS? - 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

Related

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

Masking an integer in MIPS (quick and easy)

I want to input an integer and have it be expressed in both binary and hex. Then I want to move bits 12, 13, 14, and 15 to the least significant bits of $a0, and have the output be expressed as a binary and hex. Here's my program:
.data
enter: .asciiz "Please enter your integer:\n"
binaryI: .asciiz "\nHere is the input in binary: "
hexI: .asciiz "\n\nHere is the input in hexadecimal: "
binaryO: .asciiz "\n\nHere is the output in binary: "
hexO: .asciiz "\n\nHere is the output in hexadecimal: "
.text
prompt:
li $v0, 4
la $a0, enter
syscall
li $v0, 5
syscall
add $s2, $0, $v0
li $v0, 4
la $a0, binaryI
syscall
li $v0, 35
move $a0, $s2
syscall
li $v0, 4
la $a0, hexI
syscall
li $v0, 34
move $a0, $s2
syscall
addi $t0, $0, 7
srl $s0, $s2, 12
and $s0, $s0, $t0
li $v0, 4
la $a0, hexO
syscall
li $v0, 35
move $a0, $s0
syscall
li $v0, 4
la $a0, binaryO
syscall
li $v0, 34
move $a0, $s0
syscall
li $v0, 1
add $a0, $0, $s0
syscall
li $v0, 10
syscall
For the integer 1006460, for example, the inputs and hex output work perfectly, but the binary output has an extra 5 at the end. The error I get is this:
Here is the output in binary: 0x000000055
What may have caused this extra 5 to be at the end of the output?
How silly of me. I should have deleted
li $v0, 1
add $a0, $0, $s0
syscall
from my code.

MIPS Floating Point Division Output

I am new in mips and i got this assignment that asks me to take 2 inputs from user and divide them and get a floating point output.
The problem is the output comes out like this 0.000000000, not the predicted output
this is my code
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz " a/b = "
.text
main: li $v0, 4
la $a0, str1
syscall
li $v0, 6
syscall
add $s0, $v0, $zero
li $v0, 4
la $a0, str2
syscall
li $v0, 6
syscall
move $s1, $v0
div $s0, $s1
li $v0, 4
la $a0, str3
syscall
li $v0, 2
move $a0, $t0
syscall
li $v0, 10
syscall
what should i do?
This code works for me.
.data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz " a/b = "
.text
.globl main
main:
#prompt for "a"
li $v0, 4
la $a0, str1
syscall
#User input for "a"
li $v0, 6 #The float value that is read is will be in $f0 register
syscall
mov.s $f3, $f0 #moving the value of "a" into f3 to reuse f0
#prompt for "b"
li $v0, 4
la $a0, str2
syscall
#user input for "b"
li $v0, 6 #The float value that is read is will be in $f0 register
syscall
mov.s $f4, $f0
#Dividing "a" and "b"
div.s $f12 $f3 $f4
#prompt for "a/b"
li $v0, 4
la $a0, str3
syscall
#Displaying contents of f12
li $v0, 2
syscall
li $v0, 10
syscall

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

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.

find substring and indices in mips

im trying find out substring and first occurrence indices. but something wrong. im comparing each element of pattern array and each element of string array until pointer reach to '\0'. whats the problem. algorithm is totaly wrong ?
#Note: $v0 is a symbolic name used by the assember for $2.
# $a0 is a symbolic name used by the assember for $4.
.data
prompt_str: .asciiz "Please type a text string: "
prompt_ptr: .asciiz "Please type a pattern string: "
print_yes: .asciiz "Yes, there is a match."
print_no: .asciiz "No, there is no match."
text_str: .asciiz "Text string : "
pattern_str: .asciiz "Pattern string : "
print_out: .asciiz "Output to be produced :"
print_dash: .asciiz "----------------------"
print_index: .asciiz "Starting index :"
print_msg : .asciiz "Length of longest partial match = "
nl: .asciiz "\n"
print_outer: .asciiz "please enter string"
str : .space 81
ptr : .space 81
tmp : .space 81
.text
main: la $a0, prompt_str
li $v0, 4 #print_string command.
syscall
la $a0,str #read string
li $a1,81
li $v0,8
syscall
la $t0,str #move string to $t0
la $a0,prompt_ptr
li $v0,4 #print pattern command
syscall
la $a0,ptr #read pattern
li $a1,81
li $v0,8
syscall
la $t1,ptr #move pattern to $t1
lb $t2,0($t0) #pointer first element array of string
move $t4,$t2 #address pointer of $t2
lb $t3,0($t1) #pointer first element array of pattern
outer_loop : beq $t2,$0,end_outer_loop
j inner_loop
inner_loop : beq $t2,$0,end_inner_loop
beq $t3,$0,end_inner_loop
beq $t2,$t3,end_inner_loop
addiu $t2,$t2,1
addiu $t3,$t3,1
j inner_loop
end_inner_loop :bne $t3,$0,inc_ptr
j print_match
inc_ptr : add $t2,$t4,1
j outer_loop
end_outer_loop :la $a0,print_outer
li $v0,4
syscall
print_match : la $a0,text_str #print string
li $v0,4
syscall
move $a0,$t0
li $v0,4
syscall
la $a0,nl #print newline character
li $v0,4
syscall
la $a0,pattern_str #print pattern string
li $v0,4
syscall
move $a0,$t1
li $v0,4
syscall
la $a0,nl #print newline character
li $v0,4
syscall
la $a0,print_out #print output line and newline character
li $v0,4
syscall
la $a0,nl
li $v0,4
syscall
la $a0,print_dash
li $v0,4
syscall
la $a0,print_yes
li $v0,4
syscall
la $a0,print_index #print starting index
li $v0,4
syscall
li $v0,10
syscall
end_loop : li $v0,10
syscall
I used your code for a similar project, in inner_loop you don't have a proper bne
I just put one bne and now it print just the string whit the substring..
.text
.globl main
main:
li $v0, 4
la $a0, msg1
syscall
li $v0, 8
la $a0, strMain
li $a1, 99
syscall
li $v0, 4
la $a0, msg2
syscall
li $v0, 8
la $a0, strSub
li $a1, 99
syscall
la $a0,strMain
jal findLengthString
move $a2, $v0
la $a0, strSub
jal findLengthString
move $a3, $v0 # M
sub $a2, $a2, $a3 # N-M
la $a0, strMain
la $a1, strSub
jal subStringMatch
move $t1, $v0
li $v0, 1
move $a0, $t1
syscall
exit:
li $v0, 10
syscall
lb $t9, endline
findLengthString:
li $t0, -1
move $s0, $a0
loop_fls:
lb $t1, 0($s0)
beq $t1, $t9, foundLength
addi $t0, $t0, 1
addi $s0, $s0, 1
j loop_fls
foundLength:
move $v0, $t0
jr $ra
subStringMatch:
li $t0, 0 #i
loop1:
bgt $t0,$a2, loop1done
li $t1, 0 #j
loop2:
bge $t1, $a3, loop2done
add $t3, $t0, $t1
add $t4, $a0, $t3
lb $t3, 0($t4) # main[i+j]
add $t4, $a1, $t1
lb $t4, 0($t4) # sub[j]
# if a0[i + j] != a1[j]
bne $t3, $t4, break1
addi $t1, $t1, 1
j loop2
loop2done:
beq $t1, $a3, yesReturn
j break1
yesReturn:
move $v0, $t0
jr $ra
break1:
addi $t0, $t0, 1
j loop1
loop1done:
li $v0, -1
jr $ra
.data
msg1: .asciiz "Enter Main String: "
msg2: .asciiz "Enter String to Check SubString: "
strMain: .space 100
strSub: .space 100
endline: .asciiz "\n"