-1

I had the following error in my code.Please help. Instruction references undefined symbol at 0x00400014 [0x00400014] 0x0c000000 jal 0x00000000 [main] ; 188: jal main This code convert Farenheit to Celsious and Celsius to Farenheit

.data 0x10008000

.word 5,9,32

 message1: 
    .asciiz "Select the temparature scale:<C or F><ENTER"
 message2: .asciiz "Type the desired temperature <ENTER>"
 message3: asciiz  "Temparature= "

.text

.globl main

main:
   li $v0,4
   la $a0,message1
   syscall

  li $v0,12
  syscall

  move $t0,$v0

  li $t1,70
  li$t2,67
 beq $t0,$t1,Farenheit
 beq $t0,$t2,Celcius

li $v0,10
 syscall


Farenheit:

     li $v0,4
     la $a0,message2
     syscall


     li $v0,6
     syscall

     lui $gp, 0x1000   #I put in register $gp the number 0x10008000,which                
     ori $gp, $gp,0x8000 # shows in the middle of address of static data

     lwc1 $f16, 0($gp)   

     cvt.s.w $f16, $f16



      lwc1 $f18, 4($gp)

     cvt.s.w $f18, $f18


      div.s $f20 , $f16,f$18

      lwc1 $f14, 8($gp)

      cvt.s.w $f14 $f14

      lwc1 $f12,$v0

      sub.s $f12,$f12,$f14

      mul.s $f0,$f20,$f12


       la $a0,message3
       li $v0,4
       syscall

      mov.s $f12,$f0
      li $v0,2
      syscall


       jr $ra


Celcius: 

     li $v0,4
     la $a0,message2
     syscall


     li $v0,6
     syscall

     lui $gp, 0x100
     ori $gp, $gp,0x8000

     lwc1 $f16, 0($gp)

     cvt.s.w $f16, $f16



      lwc1 $f18, 4($gp)

     cvt.s.w $f18, $f18


      div.s $f20 , $f18,f$16

      lwc1 $f14, 8($gp)

      cvt.s.w $f14 $f14

      lwc1 $f12,$v0

      mul.s $f12,$f12,$f20
      add.s $f0,$f12,$f14

       la $a0,message3
       li $v0,4
       syscall

      mov.s $f12,$f0
      li $v0,2
      syscall


       jr $ra
Jester
  • 56,577
  • 4
  • 81
  • 125
Bill kouts
  • 1
  • 1
  • 3

4 Answers4

3

Your code wouldn't even assemble cleanly.

Using $gp [with hard coded offsets] to access the .word 5,9,32 was overkill. Setting an explicit address for the .data section was probably not good either.

The "read float" syscall returns the value in $f0 and not $v0

There was a lot of replicated code between the celcius and fahrenheit sections that could be consolidated.

You were using jr $ra [return from function] without actually calling the calculation sections via jal

There should be more comments to explain the flow of your logic. You may want to see my answer here: MIPS linked list because it has some tips for asm style and clean coding.

Anyway, here's the cleaned up, annotated, and working code:

    .data

temp5:      .word       5
temp9:      .word       9
temp32:     .word       32

msg_scale:  .asciiz     "Select the temperature scale:<C or F><ENTER>"
msg_temp:   .asciiz     "Type the desired temperature <ENTER>"
msg_out:    .asciiz     "Temperature= "
msg_nl:     .asciiz     "\n"

    .text

    .globl  main

main:
    # prompt user for temp type/scale
    li      $v0,4
    la      $a0,msg_scale
    syscall

    # read in temp scale
    li      $v0,12
    syscall
    move    $t0,$v0

    # output a newline
    la      $a0,msg_nl
    li      $v0,4
    syscall

    # prompt for temperature
    li      $v0,4
    la      $a0,msg_temp
    syscall

    # read in temperature
    # NOTE: result comes back in $f0 and _not_ $v0
    li      $v0,6
    syscall
    ###lwc1 $f12,$v0
    mov.s   $f12,$f0

    lwc1    $f16,temp5              # get 5
    cvt.s.w $f16,$f16

    lwc1    $f18,temp9              # get 9
    cvt.s.w $f18,$f18

    lwc1    $f14,temp32             # get 32
    cvt.s.w $f14,$f14

    # do fahrenheit to celcius
    li      $t1,'F'
    beq     $t0,$t1,Farenheit
    li      $t1,'f'
    beq     $t0,$t1,Farenheit

    # do celcius to fahrenheit
    li      $t1,'C'
    beq     $t0,$t1,Celcius
    li      $t1,'c'
    beq     $t0,$t1,Celcius

    j       main_exit

    # print results
main_print:
    la      $a0,msg_out
    li      $v0,4
    syscall

    mov.s   $f12,$f0
    li      $v0,2
    syscall

    la      $a0,msg_nl
    li      $v0,4
    syscall

    j       main

main_exit:
    li      $v0,10
    syscall

Farenheit:
    div.s   $f20,$f16,$f18          # get 5/9
    sub.s   $f12,$f12,$f14          # subtract 32 from temp
    mul.s   $f0,$f20,$f12           # multiply by 5/9
    j       main_print

Celcius:
    div.s   $f20,$f18,$f16          # get 9/5
    mul.s   $f12,$f12,$f20          # multiply by 9/5
    add.s   $f0,$f12,$f14           # add 32
    j       main_print
Community
  • 1
  • 1
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
0

First of all thanks for the support,it was very very helpful.Sorry for my style i am beginner in assembly and generally in programming.My code was a little bit confused,cause i should complete an incomplete code.Let my explain you.This is the pronunciation:

   .data0x10008000
     .word 5,9,32,100
     .text 
      lui $gp, 0x100
     ori $gp, $gp,0x8000

     lwc1 $f16, 0($gp)

     cvt.s.w $f16, $f16



      lwc1 $f18, 4($gp)

     cvt.s.w $f18, $f18


      div.s $f20 , $f16,f$18

      lwc1 $f14, 8($gp)

      cvt.s.w $f14 $f14

      lwc1 $f12,12($gp)
      cvt.s.w $f12,$f12

      sub.s $f12,$f12,$f14

      mul.s $f0,$f20,$f12

      jr $ra

You should complete this code and add user interface (as i did in my code) and also to syntax a code which will do the convert from farheneit to celsius.That's why i got confused :(

Bill kouts
  • 1
  • 1
  • 3
  • This should be an edit to the question, or something. I don't think it's meant to be an actual answer, so please don't post it as one. – Peter Cordes May 22 '16 at 17:44
0

i had the same essay for my university(maybe you are from Auth too,if not don't give sense to this bracket:P)

In case you did not gave an answer to your problem with Craigs reply,then have a look to my code,which works properly.

I think after research (MIPS $gp register and through internet) that using gp to access data you stored in static data segment is not a good method,but that's how the program was told to be done!

Also,many errors may occur due to not reinitialising your simulator!

Here's my code,hope i gave a hand!

 .data 0x10008000  
 .word 5, 9, 32
      message1: .asciiz "Select the temparature scale:<C or F>:"
      message2: .asciiz "Type the desired temperature:"
      message3: .asciiz  "Temparature= "
      newline:  .asciiz "\n" 
.text

.globl main

main:

li $v0,4
la $a0,message1
syscall                             #prompt message

#read the character
li $v0,12
syscall

move $t0,$v0

#check the character and branch                                     
li $t1,'F'
beq $t0,$t1,Fahrenheit             
li $t1,'f'
beq $t0,$t1,Fahrenheit             #fahrenheit to celsius

li      $t1,'C'
beq     $t0,$t1,Celsius
li      $t1,'c'
beq     $t0,$t1,Celsius            #celsius to fahrenheit

li $v0,10
syscall                            #end program

Fahrenheit:

li $v0,4
la $a0,newline
syscall                           #type new line


li $v0,4                          #type message 2
la $a0,message2
syscall             

li $v0,6
syscall                          #read float from user

#gp has 0x10008000 
lui $gp, 0x1000
ori $gp, $gp, 0x8000              #load to f16 number 5

lwc1 $f16, 0($gp)                

cvt.s.w $f16, $f16                #convert to float

lwc1 $f18, 4($gp)                 #load number 9 to f18

cvt.s.w $f18, $f18                #convert to float


div.s $f20, $f16, $f18            #divide 5/9 and save it to f20

lwc1 $f14, 8($gp)                 #load number 32

cvt.s.w $f14, $f14                #convert to float


li      $v0,6                     #read from user float
mov.s   $f12,$f0                 

sub.s $f12, $f12, $f14            #Fahrenheit - 32

mul.s $f0, $f20, $f12             #(Fahrenheit-32)*5/9 ,the result

li $v0,4
la $a0,newline
syscall                           #type new line

li $v0,4
la $a0,message3
syscall                           #type message 3

mov.s   $f12,$f0
li      $v0,2
syscall                           #type the result

jr $ra

Celsius: 
li $v0,4
la $a0,newline
syscall                          #type new line


li $v0,4                         #type message 2
la $a0,message2
syscall             

li $v0,6
syscall                          #read float from user

lui $gp, 0x1000
ori $gp, $gp,0x8000

lwc1 $f16, 0($gp)                 #load number 5 to f16

cvt.s.w $f16, $f16                #convert to float

lwc1 $f18, 4($gp)                 #load number 9 to f18

cvt.s.w $f18, $f18                #convert to float

div.s $f20 , $f18,$f16            #save number 9/5( f18 / f16) to f20

lwc1 $f14, 8($gp)                 #load number 32 to f14

cvt.s.w $f14 $f14                 #convert to float

li      $v0,6                     #read from user
mov.s   $f12,$f0

mul.s $f0,$f12,$f20               #Celsius * 9/5
add.s $f0,$f0,$f14                #(Celsius*9/5)+32

li $v0,4
la $a0,message3
syscall                           #Type the Result!

mov.s   $f12,$f0
li      $v0,2
syscall

jr $ra
Community
  • 1
  • 1
Kintzo
  • 1
  • 1
  • hey there Kintzo,i'm from auth. Thanks for your help.I had already done my code ,it's quite similar with yours ,but i did two functions,check my code out and tell my if you want your opinion .Also you used $ra (return from function) without actually calling via $jal .That's a problem which had been mentioned by Craig.Here is my code – Bill kouts May 15 '16 at 11:52
-1

@kintzo hey there Kintzo,i'm from auth. Thanks for your help.I had already done my code ,it's quite similar with yours ,but i did two functions,check my code out and tell my if you want your opinion .Also you used $ra (return from function) without actually calling via $jal .That's a problem which had been mentioned by Craig.Here is my code

.data 0x10008000
     .word       5,9,32



msg_scale:  .asciiz     "Select the temperature scale:<C or F><ENTER>"
msg_temp:   .asciiz     "Type the desired temperature <ENTER>"
msg_out:    .asciiz     "Temperature= "
msg_nl:     .asciiz     "\n"

    .text

    .globl  main

main:
    # prompt user for temp type/scale
    li      $v0,4
    la      $a0,msg_scale
    syscall






  # read in temp scale
    li      $v0,12
    syscall
    move    $t0,$v0

    # output a newline
    la      $a0,msg_nl
    li      $v0,4
    syscall

    # prompt for temperature
    li      $v0,4
    la      $a0,msg_temp
    syscall

    # read in temperature
    # NOTE: result comes back in $f0 and _not_ $v0
    li      $v0,6
    syscall



    li      $t1,'F'
    beq     $t0,$t1,F


    li      $t1,'C'
    beq     $t0,$t1,C

 F: 
        jal Farenheit

    la      $a0,msg_out
    li      $v0,4
    syscall

    mov.s   $f12,$f0
    li      $v0,2
    syscall

    la      $a0,msg_nl
    li      $v0,4
    syscall


   j       main_exit


 C:
        jal Celcius


    la      $a0,msg_out
    li      $v0,4
    syscall

    mov.s   $f12,$f0
    li      $v0,2
    syscall

    la      $a0,msg_nl
    li      $v0,4
    syscall


j       main_exit



main_exit:
    li      $v0,10
    syscall





  .globl Farenheit 

   Farenheit:

    lui $gp , 0x1000
    ori $gp , $gp, 0x8000

     lwc1 $f16 , 0($gp)

     cvt.s.w $f16,$f16

    lwc1 $f18, 4($gp)

    cvt.s.w $f18, $f18

    lwc1 $f14, 8($gp)

    cvt.s.w $f14 $f14

    div.s   $f20,$f16,$f18          # get 5/9
    mov.s   $f12,$f0
    sub.s   $f12,$f12,$f14          # subtract 32 from given temprerature
    mul.s   $f0,$f20,$f12           # multiply by 5/9
    jr      $ra







 .globl Celcius 

  Celcius:

    lui $gp , 0x1000
    ori $gp , $gp, 0x8000

     lwc1 $f16 , 0($gp)

     cvt.s.w $f16,$f16

    lwc1 $f18, 4($gp)

    cvt.s.w $f18, $f18

    lwc1 $f14, 8($gp)

    cvt.s.w $f14 $f14


    div.s   $f20,$f18,$f16          # get 9/5
    mov.s   $f12,$f0
    mul.s   $f12,$f12,$f20          # multiply by 9/5
    add.s   $f0,$f12,$f14           # add 32
    jr      $ra
Bill kouts
  • 1
  • 1
  • 3
  • This appears to be a reply to @Kinto's, but apparently it's also an answer to your own question since you say this code works properly. It would be better to post this as a proper stand-alone answer, and leave a comment on Kinto's answer to let him know you'd posted this. (Having a discussion in the text of your answer is not how we do things on SO). – Peter Cordes May 22 '16 at 17:47