Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[MIPS] How to convert hex to decimal?

convert hex to decimal

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1 azi

azi

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 11 October 2014 - 10:48 AM

how to convert 16-bit hexa-to-decimal ?sign or unsigned?

 

heres what i have wrote so far?

any help is appreciated

   .data
buffer: .space  8
prompt:    .asciiz "Enter hexadecimal number: "
ans1:    .asciiz "Decimal is "

 
 
    .text        
 main:
    la $a0,prompt    # print prompt on terminal
    li $v0,4
    syscall

       li   $v0, 8   # get input
       la   $a0, buffer    # load byte space into address
       li   $a1, 2         # allot the byte space for string
       move $t0,$a0      # save string to t0
       syscall
    

hextodec:    slti $t0,$a0,0x41
             beq  $t0,$zero,letter
             addi $v0,$a0,-0x30
             j end
 letter:
            addi $t0,$a0,-0x41
        addi  $v0,$t0,10
end:
           jr $ra

Attached Files


Edited by dargueta, 11 October 2014 - 02:48 PM.


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 11 October 2014 - 02:59 PM

A number of observations:

 

1) You cannot expect any of the t or a registers ($t0, $t1... and $a0, $a1... etc.) to keep their value across function calls or system calls. For hextodec to work, you have to reload $a0 with the address of buffer using the la instruction.

 

2) You convert the integer right but you don't print it out at all. All you're doing is setting $v0 to your result and exiting. If you want to print it out, use the print_int system call ($v0 = 1) to do that.


sudo rm -rf / && echo $'Sanitize your inputs!'


#3 azi

azi

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 14 October 2014 - 09:47 PM

i think i got the second part but i dont know how to do the first part.would u please explain what excatly should i do?



#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 15 October 2014 - 10:08 AM

Put la $a0, buffer as the first line of hextodec.


Edited by dargueta, 15 October 2014 - 10:09 AM.
Formatting

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 azi

azi

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 15 October 2014 - 11:38 AM

thanks



#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 15 October 2014 - 11:51 AM

No problem. Does that fix everything?


sudo rm -rf / && echo $'Sanitize your inputs!'


#7 azi

azi

    CC Lurker

  • New Member
  • Pip
  • 8 posts

Posted 15 October 2014 - 11:59 AM

no it dosnt. still not working :(

 

is the following code correct ?

li $v0,1
move $a0, $v0
syscall

Edited by dargueta, 15 October 2014 - 12:43 PM.


#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 15 October 2014 - 12:42 PM

No. Note that you just got rid of whatever was in $v0 when you stored 1 into it. To get the proper value into $a0 you want to switch the two operations.

move    $a0, $v0
li      $v0, 1
syscall

sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download