Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Homework] [MIPS] Need help entering integers

assembly mips integer array

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

#1 supermastereu

supermastereu

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 26 April 2014 - 07:32 AM

Hi. I need help to write one program in Assembly Mips.

 

<< read the characters typed by the user and store these characters a string. This string is later converted to an integer. If the user tries introducing non-valid characters (that is, other than the numerals 0 to 9) the program "complaining" and start all over.
again.>>
 
.data

valor:    .asciiz ""
str1:    .asciiz "Plesar enter a number:\n"
str2:    .asciiz "\nError! \n"
str3:    .asciiz "The number is: "
strteste: .asciiz "\n"

    .text        
  .globl main    
    
main:  
la $a0,str1      #

....

Edited by supermastereu, 26 April 2014 - 07:33 AM.


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 26 April 2014 - 05:22 PM

What specifically do you need help with? We don't do homework for you here.


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


#3 supermastereu

supermastereu

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 26 April 2014 - 06:22 PM

                  .data   
            
text:          .asciiz "\nEnter a number : "
text2:       .asciiz "\nThe value entered was: "    
text3:       .asciiz "\nEntered an invalid character"
val:          .space 160

            .text                  
main:       li $v0, 4               
               la $a0, tex    
               syscall                

main2:      li $v0, 12               
                syscall                 
                sw    $v0, val
            
            
if_1:    lb    $t2, val            
           blt    $t2, 48, if_2    
        
if_2:    bgt $t2, 57, imp2    
           b    main2    
                
if_3:    lb    $t2, val             
           bge    $t2, 48, if_4    
        
if_4:    ble    $t2, 57, imp1    

imp1:    li    $v0, 4          
            la    $a0, text2
            syscall                    

           li     $v0, 11                
           lw     $a0, val
           syscall    
        
        b end      

            
imp2:    li    $v0, 4          
            la    $a0, text3
            syscall                    
        
        b    main                
        
end:                
            li $v0, 10              
            syscall

When i run this code the system give me a error...  

 

Instruction references undefined symbol at 0x00400014

[0x00400014] 0x0c000000 jal 0x00000000 [main] ; 188: jal main



#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 26 April 2014 - 06:25 PM

Put .globl main at the top of your program.


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


#5 supermastereu

supermastereu

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 26 April 2014 - 07:54 PM

Now i have the error...

Exception occurred at PC=0x0040003c

 Unaligned address in store: 0x1001004a



#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 26 April 2014 - 09:00 PM

All word-sized memory accesses (lw and sw) need to be aligned on a four-byte boundary, meaning you can only read from or write to addresses that are multiples of 4. These end in 0, 4, 8, or c; var is at 0x1001004a which is not aligned to a four-byte boundary.

 

Either move val to the beginning of your data section or put .align 2 (align to 2bytes) right before it. 


Edited by dargueta, 26 April 2014 - 09:05 PM.

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


#7 supermastereu

supermastereu

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 27 April 2014 - 05:14 PM

First, thanks for your time and help.

Now, the program print the message if the character is not valid but don't print the integer when character is valid.

.data   
val:          .space 160

text:          .asciiz "\nEnter a number : "
text2:       .asciiz "\nThe value entered was: "    
text3:       .asciiz "\nEntered an invalid character"


            .text                  
main:       li $v0, 4               
               la $a0, text    
               syscall                

main2:      li $v0, 12               
                syscall                 
                sw    $v0, val
            
            
if_1:    lb    $t2, val            
           blt    $t2, 48, if_2    
        
if_2:    bgt $t2, 57, imp2    
           b    main2    
                
if_3:    lb    $t2, val             
           bge    $t2, 48, if_4    
        
if_4:    ble    $t2, 57, imp1    

imp1:    li    $v0, 4          
            la    $a0, text2
            syscall                    

           li     $v0, 11                
           lw     $a0, val
           syscall    
        
        b end      

            
imp2:    li    $v0, 4          
            la    $a0, text3
            syscall                    
        
        b    main                
        
end:                
            li $v0, 10              
            syscall


#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 27 April 2014 - 10:01 PM

if_1:   lb    $t2, val            
        blt    $t2, 48, if_2

I'm pretty sure this should jump to imp2, not if_2. if_2 is the next label so either way it's jumping to the same address.

 

Same deal here:

if_3:    lb    $t2, val             
           bge    $t2, 48, if_4    
        
if_4:    ble    $t2, 57, imp1  

I'm pretty sure you don't need these at all. You should only have to check for < 48 and > 57.


Edited by dargueta, 27 April 2014 - 10:05 PM.

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


#9 supermastereu

supermastereu

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 28 April 2014 - 09:30 AM

.data 

buffer: .space 160

str1:   .asciiz "\n Please enter a number : "

str2:   .asciiz "\n The value entered was:"

str3:   .asciiz "\n Entered an invalid character"

.text

.globl  main 

main:

     la $a0,str1     
     li $v0,4
     syscall


main2:      
          li  $v0, 12               
          syscall 
          sw  $v0, buffer 

            
if_1:     lb  $t2, buffer           
          blt $t2, 48, if_2    
        
if_2:    bgt  $t2, 57, imp2    
           

if_3:    lb   $t2, buffer             
         bge  $t2, 48, if_4    
        
if_4:    ble  $t2, 57, imp1              
               

imp1:     

        la   $a0, str2
        li   $v0, 4          
        syscall          
        
        la   $a0, buffer              
        li   $v0, 4
                            
        syscall
        b end
             
     
imp2:    
         li    $v0, 4          
         la    $a0, str3
         syscall                  
        
        b    main     
        
end:
     li $v0,10      
     syscall

This code works... but only for one character...


Edited by supermastereu, 28 April 2014 - 09:31 AM.


#10 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 28 April 2014 - 09:36 AM

In that case you're gonna want to use syscall 5. Here's a list of the valid syscalls:

 

http://courses.misso...yscallHelp.html


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


#11 supermastereu

supermastereu

    CC Lurker

  • New Member
  • Pip
  • 7 posts

Posted 28 April 2014 - 01:11 PM

Can't... 



#12 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 28 April 2014 - 04:05 PM

Ah, I see now. Okay. First of all, this whole if_1, if_2 thing is unnecessary. You're confusing yourself and jumping to the wrong labels. All you need to check the validity of a character is this, right after main2 (no label necessary)

lb      $t2, buffer           
blt     $t2, 48, imp2    
bgt     $t2, 57, imp2

After that you're guaranteed to have a valid character in $t2 and you can continue converting your number. I don't see you doing the conversion anywhere, though.


Edited by dargueta, 28 April 2014 - 04:06 PM.

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





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