Go4Expert (http://www.go4expert.com/)
-   Assembly Language Programming (ALP) Forum (http://www.go4expert.com/forums/assembly-language-programming-forum/)
-   -   itoa procedure (assembly MIPS) (http://www.go4expert.com/forums/itoa-procedure-assembly-mips-t27438/)

UnSourCeR 25Dec2011 20:03

itoa procedure (assembly MIPS)
Hello, that's my 1st post in your forum :). I have an assignment in assembly MIPS ( i'm not asking for spoon feeding or to make some1 else to do my homework just some tips and suggestions).

Assignment : Write a procedure (itoa) in assembly MIPS, that converts an integer to a null-terminated ASCII String. Procedure has as arguments in $a0 the integer and in $a1 a pointer to the memory where you should save the string that you produce. Procedure saves in $v0 the number of chars of string (expect the null).

My Solution:


        addi $t0,$zero,10 # t0=10
        addi $t1,$t1,a0  # t1=a0
                  div $t1,$t0    #t1/10
                  mflo $t1      #t1 = quotient
                  mfhi $t2      #t2 =remainder
                  addi $t2,$t2,0x30 #Convert to ASCII
                  addi $sp,$sp,-1 #Make space for 1 byte in the stack
                  sb $t2,0($sp) #Push t2 in the stack
                  addi $v0,$v0,1 #v0=v0+1
                  bne $t1,$zero,Loop #If t1<>0 go to Loop
                  sw $t0,$v0  #t0=v0
                  lb $t1,0($sp) #pop the last byte for the stack
                  addi $sp,$sp,1 #Reduce the stack size by 1 byte
                  add $t2,$v0,-$t0 #t2=v0-t0
                  sb $t1,$t2($a1) # savebyte to the proper location of memory
                  addi $t0,$t0,-1 #t0=t0-1
                  bne $to,$zero,order #If t0<>0 go to order
        sb 0x0,$v($a0) # add null character to the end of the string
    jr $ra

Soz for my bad english, is this solution right? Do you have any tips for simpler code?Thanks in advance

UnSourCeR 26Dec2011 16:05

Re: itoa procedure (assembly MIPS)
Any tips? in second line i made mistake it is add not addi.

All times are GMT +5.5. The time now is 10:38.