1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

itoa procedure (assembly MIPS)

Discussion in 'Assembly Language Programming (ALP) Forum' started by UnSourCeR, Dec 25, 2011.

  1. UnSourCeR

    UnSourCeR New Member

    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
  2. UnSourCeR

    UnSourCeR New Member

    Any tips? in second line i made mistake it is add not addi.

Share This Page