how can I write a MIPS assembly code to write a function - function

Hi I've just started learning MIPS codeing and I'm really stuck on this, I want to write a function called isCapital, that tests whether a character ch is capitalcase or not?
Please help, Thank you.

.data
lowerCaseChar: .asciiz "Lower case character"
upperCaseChar: .asciiz "Upper case character"
notAlphabeticChar: .asciiz "Not alphabetic character "
char : .byte 'b' # type a character what you want
.text
.globl main
main:
jal isCapital
exit:
li $v0, 10
syscall
isCapital:
addi $sp,$sp,-4
sw $ra,0($sp)
lbu $s0,char
sge $t1,$s0,65
sle $t2,$s0,90
and $t3,$t1,$t2
beq $t3,1,upperCase
sge $t1,$s0,97
sle $t2,$s0,122
and $t3,$t1,$t2
beq $t3,1,lowerCase
j notAlphabetic
end:
li $v0,4
syscall
lw $ra,0($sp)
addi $sp,$sp,4
jr $ra
lowerCase:
la $a0,lowerCaseChar # system call 4 for print lower case
j end
upperCase:
la $a0,upperCaseChar # system call 4 for print upper case
j end
notAlphabetic:
la $a0,notAlphabeticChar
j end

Related

How to store the input correctly in MIPS?

Although I have try to input different value ,but the Qtspim still print out the value"0" and the input is also not saved in the register ,I try to find the answer in internet,but it seem like no one have face this problem before.I do it work for 4hrs, I FEEL very frustrated with this.
here is my code ,I try to define whether the input is prime number or not.
# Power program
# ----------------------------------
# Data Segment
.data
request: .asciiz "Enter an integer:"
Yes: .asciiz " is a prime number.\n"
No: .asciiz " is not a prime number\n."
End: .asciiz"End!\n"
Key: .word -1
One: .word 1
# ----------------------------------
# Text/Code Segment
.text
.globl main
main:
lw $t1,One
add $s2,$zero,$zero
lw $s3,One
lw $s0,Key
add $s5,$zero,$zero
j power # call the function power
li $v0, 10 # call code for exit
syscall # system call
.end main
.globl power
power:
j power_loop
power_loop:
la $a0, request # load string request in memory to $a0
li $v0, 4 # print out the string
syscall #prevent Memory address out of bounds
li $v0, 5
syscall
move $s7,$v0
beq $s7, $s0, exit_L #when input =-1 exit
j Prime #otherwise go to define the input whether prime num
Prime:
beq $s7,$t1,result
div $s7,$t1
mthi $t4
beq $t4,$zero,count
addi $t1,$t1,1
j Prime
count: #counting the number of factors
addi $s2,1
addi $t1,$t1,1
result: addi $t1,$zero,1
bne $s2,$s3,resultNo
beq $s2,$s3,resultYes
resultYes: li $v0,1
move $a0,$s7
syscall
add $s2,$zero,$zero
la $a0,Yes
li $v0,4
syscall
j power_loop
resultNo: li $v0,1
move $a0,$s7
syscall
add $s2,$zero,$zero
la $a0,No
li $v0,4
syscall
j power_loop
exit_L:
la $a0,End
li $v0,4
syscall
jr $ra
.end power

MIPS Output Error

I have an unknown output error after entering 2nd value which is after entering base number.
Hopefully,some of you could identify my error:
ERROR:Instruction references undefined symbol at 0x00400060
[0x00400060] 0x102a0000 beq $1, $10, 0 [hex-0x0040005c]
PROGRESS:Currently stucked at Step 2.
What i want to do is,
1)User enter a decimal value
2)User enter type of conversion
3)Go to desired subroutine depending on type of conversion chosen earlier
4)Display output
.data
prompt: .asciiz "Enter the decimal number to convert: "
base: .asciiz "Select type of base (2 for binary,16 for hexadecimal or 8 for octal): "
ans1: .asciiz "\nBinary Output is equivalent:"
ans2: .asciiz "\nOctal Output is equivalent:"
ans3: .asciiz "\nHexadecimal Output equivalent:0x"
result1: .space 8
.text
.globl main
main:
la $a0, prompt #Display message
li $v0, 4
syscall
li $v0, 5
syscall
beq $v0, $zero, Exit #Exit if 0 decimal is entered
move $t0, $v0 #Else copy value entered into temporaries
askbase:
li $v0, 4
la $a0, base #Display message
syscall
li $v0, 5
syscall
add $t1,$zero,$v0 #Add desired value/base entered into t1
beq $t2,16,hex #if base 16 is entered,goto hex subroutine
beq $t2,8,oct
beq $t2,2,bin
la $a0, ans3
li $v0, 4
syscall
li $t0, 8 # counter
la $t3, result1 # where answer will be stored
Hex:
beqz $t0, Exit # branch to exit if counter is equal to zero
rol $t2, $t2, 4 # rotate 4 bits to the left
and $t4, $t2, 0xf # mask with 1111
ble $t4, 9, Sum # if less than or equal to nine, branch to sum
addi $t4, $t4, 55 # if greater than nine, add 55
b End
Sum:
addi $t4, $t4, 48 # add 48 to result
End:
sb $t4, 0($t3) # store hex digit into result
addi $t3, $t3, 1 # increment address counter
addi $t0, $t0, -1 # decrement loop counter
j Loop
Exit: la $a0, result1
li $v0, 4
syscall
la $v0, 10
syscall
It looks like you have a typo at line 29 beq $t2,16,hex should be beq $t2,16,Hex. Note the capital letter on Hex. You also have a number of undefined labels: Loop, oct, bin... Without those labels you are going to have issues. It is a good idea to just set some up as place holders (until you have their subroutines defined). Maybe have them all branch to Exit:. The assembler cannot resolve your branch instructions without having the actual labels to go to.
You haven't stored anything in $t7, so there's no particular reason to expect that $t7 would equal 16.
What you probably wanted to write is:
beq $t1,16,hex
Since $t1 is the register that you stored the base in.
However, I really don't see why you would want that jump with the way the code currently is structured. The hex subroutine relies on a few registers (like $t0 and $t3) to have been initialized to certain values, and that initialization would be skipped if that beq is taken.

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

Take user input and print a floating point array in MIPS

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.

MIPS - compare input string to one stored in memory

I have a functioning string compare method written in MIPS (bit by bit comparison of two strings from an input by the user), but I'm trying to update it to compare the second input with one that I have stored in memory. (If those two are equal, I want to use the first string somewhere else).
However, I'm running into some issues. Here's my code:
.data
str1: .space 20
str2: .space 20
msg1:.asciiz "Please enter string (max 20 characters): "
msg2: .asciiz "\n Please enter method (max 20 chars): "
msg3:.asciiz "\nSAME"
msg4:.asciiz "\nNOT SAME"
.text
.globl main
main:
li $v0,4 #loads msg1
la $a0,msg1
syscall
li $v0,8
la $a0,str1
addi $a1,$zero,20
syscall #got string to manipulate
li $v0,4 #loads msg2
la $a0,msg2
syscall
li $v0,8
la $a0,str2
addi $a1,$zero,20
syscall #got string method
la $a0,str1 #pass address of str1
la $a1,str2 #pass address of str2
jal methodComp #call methodComp
beq $v0,$zero,ok #check result
li $v0,4
la $a0,msg4
syscall
j exit
ok:
li $v0,4
la $a0,msg3
syscall
exit:
li $v0,10
syscall
methodComp:
add $t0,$zero,$zero
add $t1,$zero,$a0
add $t2,$zero,$a1
loop:
lb $t3($t1) #load a byte from each string
lb $t4($t2)
beqz $t3,checkt2 #str1 end
beqz $t4,missmatch
slt $t5,$t3,$t4 #compare two bytes
bnez $t5,missmatch
addi $t1,$t1,1 #t1 points to the next byte of str1
addi $t2,$t2,1
j loop
missmatch:
addi $v0,$zero,1
j endfunction
checkt2:
bnez $t4,missmatch
add $v0,$zero,$zero
endfunction:
jr $ra
Essentially, what I was hoping to do was declare at the beginnning
strToCompare: .asciiz "foo"
and change where I pass the adress of str1 from
la $a0,str1 #pass address of str1
to
la $a0,strToCompare #pass address of strToCompare
Does anyone know why this would not work, or a better way to implement this?
Thanks a bunch, sorry if my formatting is off, this is my first time posting.
You are missing the line feed character (\n) that your input method (the system call you are using to input data from the user) is adding at the end.
You can either preprocess your input string to remove that line feed or add it to your stored string.
The latter is easy, just change
strToCompare: .asciiz "foo"
to
strToCompare: .asciiz "foo\n"