MIPS Functions and arrays

By : Zein
Source: Stackoverflow.com
Question!

I can understand and use Java/c++ to a good extent, but for the life of me assembly just confuses me there are 2 functions I'm having trouble with. First:

One function that receives a string and prints it on the terminal

And another one that receives a string and converts it to integers (Strings given all made of numbers).

Any idea on where to start?

Update

On the second function, so far I got this:

main:   
atoi:
    li      $v0, 8
    la      $a0, tstr
    li      $a1, 64
    syscall
    sub $sp, $sp,4
    sw $ra, 0($sp)
    move $t0, $a0
    li $v0, 0
next:
    lb $t1, ($t0)
    beqz  $t1, endloop
    mul $v0, $v0, 10
    add $v0, $v0, $t1
    sub $v0, $v0, 48
    add $t0, $t0, 1
    b next

endloop:
    lw $ra, 0($sp)
    add $sp, $sp, 4

Updated code, still getting the error on 10 being an invalid operand. And about sub $v0, $v0, 48 should I just do it as sub $t1, $t1, 48?

By : Zein


Answers

For input-output stuff, you have to use system calls. For writing (zero-terminated) strings you'll use syscall #4, which wants the address of the buffer in $a0. Now just place the umber of the syscall in $v0 and execute it. For example, this snippet reads a string:

li      $v0, 8                # syscall number
la      $a0, theString        # buffer
li      $a1, 64               # length of buffer
syscall                       # read!

Here you can find some syscalls numbers.
For the second exercise, here's the C code, try to translate it :P

int result = 0;

while (*digit 
By : BlackBear


This video can help you solving your question :)
By: admin