Can someone help me see my code why is such an error?
I'm doing decimal conversion to binary and here is my code
.data
msg1: .asciiz "Please insert value (A > 0) : "
msg2: .asciiz "Please insert the number system B you want to
convert to (2<=B<=10): "
#Above sting must be in one line
msg3: .asciiz "\nResult : "
.text
.globl main
main:
addi $s0,$zero,2
addi $s1,$zero,10
getA:
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
blt $v0,$zero,getA
move $t0,$v0
getB:
li $v0,4
la $a0,msg2
syscall
li $v0,5
syscall
blt $v0,$s0,getB
bgt $v0,$s1,getB
add $t1,$zero,$v0
li $v0,4
la $a0,msg3
syscall
add $a0,$zero,$t0
add $a1,$zero,$t1
jal convert
li $v0,10
syscall
convert:
#a0=A
#a1=B
addi $sp,$sp,-16
sw $s3,12($sp) #counter,used to know
#how many times we will pop from stack
sw $s0,8($sp) #A
sw $s1,4($sp) #B
sw $ra,0($sp)
add $s0,$zero,$a0
add $s1,$zero,$a1
beqz $s0,end
div $t4,$s0,$s1 #t4=A/B
rem $t3,$s0,$s1 #t3=A%B
add $sp,$sp,-4
sw $t3,0($sp) #save t3
add $a0,$zero,$t4 #pass A/B
add $a1,$zero,$s1 #pass B
addi $s3,$s3,1
jal convert #call convert
end:
lw $ra,0($sp)
lw $s1,4($sp)
lw $s0,8($sp)
lw $s3,12($sp)
beqz $s3,done
lw $a0,16($sp)
li $v0,1
syscall
done:
addi $sp,$sp,20
jr $ra #return
and when I run this code it said syntax error at line 13 which is li $v0,4
can someone tell me what is happening?
Related
sorry, I try to multiply two integers but it doesn't work. I can't find where the problem is.Maybe because of register' names but I don't know how to correct it. I correct many times but it is not successful. Could anyone give me some points?
.data
prompt1: .asciiz "Please enter the first signed (decimal) integer: "
prompt2: .asciiz "Please enter the second signed (decimal) integer: "
result_msg: .asciiz "The result of these two 16-bit integers\' multiplication is: "
.text
.globl main
main:
li $v0, 4 #print prompt
la $a0, prompt1
syscall
li $v0, 5 #read multiplicand
syscall
move $s0, $v0
li $v0, 4 #print prompt
la $a0, prompt2
syscall
li $v0, 5 #read multiplier
syscall
move $s1, $v0
Mult: ori $t0,$zero,1 #mask
move $s3, $0 #initialize the result register
move $t1, $0
loop: beq $s1, $zero, end #if the multiplier is 0 then finished
and $t1, $t0, $s1 #mask
beq $t1, 1, mult_add
beq $t1, 0, shift
mult_add: addu $s3, $s3, $s0 #add to get product
shift:
sll $s0, $s0, 1 #shift multiplicand left
srl $s1, $s1, 1 #shift multiplier right
j loop
end:
jr $ra
result: #input the print_string
li $v0, 4
la $a0, result_msg
syscall
exit:
li $v0, 1 #input result
move $a0, $s3
syscall
li $v0, 10 #exit
syscall
Inspecting your code I see that you jump to label end when you are done multiplying.
The instruction at that label issues a jr $ra which "returns from a function", but I guess you just want to print the result and exit.
Therefore I'd suggest you remove that instruction so as to print the result and exit and maybe remove label result as it is not used anywhere in your code.
I'm writing a MIPS program that is supposed to ask a user for two numbers, then add, subtract, multiply, and divide those two numbers. I am required to use functions. Whenever I execute my program my add/subtract functions work but for some reason when the program gets to my multiplication function it wont print the answer and MARS gives me the message "dropped off bottom".
.text
main:
la $a0,prompt1
li $v0,4
syscall #Asks for first integer
li $v0,5
syscall #stores first int in $v0
add $s0,$v0, $zero
la $a0,prompt2
li $v0,4
syscall #puts first int into $t0, asks for second int.
li $v0,5
syscall #stores second int in $v0
move $s1,$v0 #moves second int into $t1
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal add #jumps to add method
move $t2, $v0 #moves the result of add method into $t2
la $a0,ans1
li $v0,4
syscall # print string before result
move $a0,$t2
li $v0,1
syscall # print result of sum
la $a0,endl
li $v0,4
syscall #prints blank line
#--------------------------------------------SUB
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal sub #jumps to sub method
move $t0, $v0 #Moves answer from sub method to $t0
la $a0,ans2
li $v0,4
syscall # print string before result
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------mult
la $a0,ans3
li $v0,4
syscall # print string before result
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal mult
move $t0, $v0 #moves answer into $a0
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------
li $v0,10
syscall #ENDS PROGRAM
add:
move $t0, $a0
move $t1, $a1
add $v0, $t0, $t1
jr $ra #jumps back
sub:
move $t0, $a0
move $t1, $a1
sub $v0, $t0, $t1
jr $ra # jumps back
mult:
move $t0, $a0
move $t1, $a1
mult $t0, $t1 #multiplies $t0,$t1
mfhi $a0 #overflow
mflo $v0 #answer
.text
main:
la $a0,prompt1
li $v0,4
syscall #Asks for first integer
li $v0,5
syscall #stores first int in $v0
add $s0,$v0, $zero
la $a0,prompt2
li $v0,4
syscall #puts first int into $t0, asks for second int.
li $v0,5
syscall #stores second int in $v0
move $s1,$v0 #moves second int into $t1
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal add #jumps to add method
move $t2, $v0 #moves the result of add method into $t2
la $a0,ans1
li $v0,4
syscall # print string before result
move $a0,$t2
li $v0,1
syscall # print result of sum
la $a0,endl
li $v0,4
syscall #prints blank line
#--------------------------------------------SUB
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal sub #jumps to sub method
move $t0, $v0 #Moves answer from sub method to $t0
la $a0,ans2
li $v0,4
syscall # print string before result
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------mult
la $a0,ans3
li $v0,4
syscall # print string before result
move $a0, $s0 #moves first int into $a0
move $a1, $s1 #moves second int into $a1
jal mult
move $t0, $v0 #moves answer into $a0
move $a0, $t0 #moves answer from sub method $t0 into $a0 to be printed.
li $v0, 1
syscall#prints result of difference
la $a0,endl
li $v0,4
syscall #prints blank line
#------------------------------------------
li $v0,10
syscall #ENDS PROGRAM
add:
move $t0, $a0
move $t1, $a1
add $v0, $t0, $t1
jr $ra #jumps back
sub:
move $t0, $a0
move $t1, $a1
sub $v0, $t0, $t1
jr $ra # jumps back
mult:
move $t0, $a0
move $t1, $a1
mult $t0, $t1 #multiplies $t0,$t1
mfhi $a0 #overflow
mflo $v0 #answer
li $v0,10
syscall
.data
prompt1:.asciiz "Enter the first integer: "
prompt2:.asciiz "Enter the second integer: "
ans1: .asciiz "The sum is "
ans2: .asciiz "The difference is "
ans3: .asciiz "The product is "
ans4: .asciiz "The quotient is "
endl:.asciiz "\n"
.data
prompt1:.asciiz "Enter the first integer: "
prompt2:.asciiz "Enter the second integer: "
ans1: .asciiz "The sum is "
ans2: .asciiz "The difference is "
ans3: .asciiz "The product is "
ans4: .asciiz "The quotient is "
endl:.asciiz "\n"
I added
li $v0, 10 #10 is the system call to exit
sys call
and this fixed the "drop off bottom" error for me
Can someone look through my codes and see if anything wrong that has been causing me errors?
**Error I received:**spim:(parser) Label is defined for the second time on line 8 of file C:/Users/Desktop/test5.asm main:
^
Is the error trying to say that i have 2 "main" word in my code? I tried removing one of it..but failed.
.data
msg1: .asciiz "Please insert value (A > 0) : "
msg2: .asciiz "Please insert the number system B you want to
convert to (2<=B<=10): "
#Above sting must be in one line
msg3: .asciiz "\nResult : "
.text
.globl main
main:
addi $s0,$zero,2
addi $s1,$zero,10
getA:
li $v0,4
la $a0,msg1
syscall
li $v0,5
syscall
blt $v0,$zero,getA
move $t0,$v0
getB:
li $v0,4
la $a0,msg2
syscall
li $v0,5
syscall
blt $v0,$s0,getB
bgt $v0,$s1,getB
add $t1,$zero,$v0
li $v0,4
la $a0,msg3
syscall
add $a0,$zero,$t0
add $a1,$zero,$t1
jal convert
li $v0,10
syscall
convert:
#a0=A
#a1=B
addi $sp,$sp,-16
sw $s3,12($sp) #counter,used to know
#how many times we will pop from stack
sw $s0,8($sp) #A
sw $s1,4($sp) #B
sw $ra,0($sp)
add $s0,$zero,$a0
add $s1,$zero,$a1
beqz $s0,end
div $t4,$s0,$s1 #t4=A/B
rem $t3,$s0,$s1 #t3=A%B
add $sp,$sp,-4
sw $t3,0($sp) #save t3
add $a0,$zero,$t4 #pass A/B
add $a1,$zero,$s1 #pass B
addi $s3,$s3,1
jal convert #call convert
end:
lw $ra,0($sp)
lw $s1,4($sp)
lw $s0,8($sp)
lw $s3,12($sp)
beqz $s3,done
lw $a0,16($sp)
li $v0,1
syscall
done:
addi $sp,$sp,20
jr $ra #return
Solved.TQ
Turned out that the SPIM compiler was messed up
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"
I need to take input from the user a floating point array and then print it. I tried the following code:-
.text
.globl main
main:
la $s0,size
lw $s1,0($s0) # size in $s1
ori $s2,$zero,0 # i in $s2
la $s3,arr # arr in $s3
li $v0,4
la $a0,msg1
syscall
L1:
beq $s2,$s1,DONE
li $v0,6
syscall
swc1 $f0,0($s3)
j UPDATE
UPDATE:
addi $s3,$s3,4
addi $s1,$s1,1
j L1
DONE:
li $v0,4
la $a0,msg2
syscall
la $t0,arr
ori $t1,$zero,0
L2:
beq $t1,$s1,EXIT
lwc1 $f20,0($t0)
li $v0,2
mov.s $f12,$f20
syscall
addi $t0,$t0,4
addi $t1,$t1,1
j L2
EXIT:
li $v0,10
syscall
.data
size: .word 9
arr: .float 0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
msg1: .asciiz "Enter the elements:-"
msg2: .asciiz "The elements are:-"
When i give the input, there is a runtime exception 'invalid float input' at syscall 6. Please help!!!
I guess you are using , as the decimal point instead of ..
E.g.: instead of entering 3.14159 you are entering 3,14159 which is not expected at least by MARS.
Aside from that, I think the line addi $s1,$s1,1 in your code should be addi $s2,$s2,1, as you seem to be using $s2 to hold the current value of your index counter.