Hack Forums
[Assembly] Simple MIPS program - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Assembly] Simple MIPS program (/showthread.php?tid=5796985)



Simple MIPS program - Latios - Feb 8, 2018

I am attempting to write a program that simply counts the number of characters and words in a string and I'm about to end it all.

This is what I have so far:

Code
.data
inputPromptString:    .asciiz        "Enter some text"
            .align 2
userInputString:    .asciiz        ""
            .align 2
words:            .word        0
characters:        .word        0

.text
main:    
    la $a0, inputPromptString    #load the address of the inputpromptstring into a0
    la $a1, userInputString        #load address of the string the user inputs
    li $v0, 54            #prompt for user input
    syscall
    
    la $s0, userInputString        #save the user input into $s0
    la $t1, userInputString
    jal count
    
    sw $v0, characters    #store variable into characters
    sw $v1, words        #store variable into words


exit:    
    li $v0, 10
    syscall
    
        
count:
    li $v0, 0
    li $v1, 0
    
    loop:
        lb $t2, ($t1)
        beqz $t2, exitLoop
        addi $v0, $v0, 1
        addi $t1, $t1, 1
        beq $t2, 32, addWord
        
        j loop
        
    addWord:
        addi $v1, $v1, 1
        j loop
        
    exitLoop:
        addi $v1, $v1, 1
        jr $ra

For whatever reason, stepping through showed me that it's not loading anything into $t2 (lb $t2, ($t1) does nothing), and then it does the beqz comparison and since there's nothing loaded, it exist the loop. Maybe someone can see an issue I'm missing here but I'm literally pulling my hair out over this.


RE: Simple MIPS program - 6577657534735 - Feb 8, 2018

What emulator are you running? Can't get this to run anywhere

-Are you sure it's that instruction messing up or earlier ones being wrong?
-Does $t1 have the right address or a junk one pointing to empty bytes?
-Try explicitly setting an offset of 0 on $t1 like 0($t1)
-How are you determining that it's not setting the value

Backtrack from the first point where you know something is wrong. Every instruction should have very simply side effects so it should be apparent.


RE: Simple MIPS program - Latios - Feb 9, 2018

(Feb 8, 2018 09:53 PM)Abraluna Wrote:  What emulator are you running?  Can't get this to run anywhere

-Are you sure it's that instruction messing up or earlier ones being wrong?
-Does $t1 have the right address or a junk one pointing to empty bytes?
-Try explicitly setting an offset of 0 on $t1 like 0($t1)
-How are you determining that it's not setting the value

Backtrack from the first point where you know something is wrong.  Every instruction should have very simply side effects so it should be apparent.
I got it figured out. I'm using MARS.

Basically I'm an idiot and assumed that when loading 54 into $v0 and doing a syscall, the parameter for the number of bytes it should read would default, so I didn't load a number. When I loaded a number of bytes to read it started working correctly.

I appreciate your response though.