1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Rep Power

    Sw $rs, imm($rx) (store word)

    Hi to you all...!
    I am new to assembly, and I have a small problem with sw(store)
    I can't figure how is the proper use of this instruction. I want to store the integer I've read to an array of integers.
    My code follows, I get an 'Exception 7 [Bad data address] occured and ignored' in sw $19, 16($10) # STORES
    How should I declare sw(at the end of the code)???

    thank you in advance!

             # register $7 : .,.,.,.,
            # register $16: Array Start Address
            # register $17: i
            # register $18: j
            # register $19: tmp
            # register $20: tmp2
            # register $26: 8, The number of the values
            # register $27: 1
            # register $28: x
        .align 4
        Array1: .space 32 #Array1 can store 8 integers(each 4 bytes)
    str_n:      .asciiz "Dwse enan akeraio!"
    str_ln:     .asciiz "\n"
    str_sp:     .asciiz " "
    str_hyphen: .asciiz "---------------------"
        .text           # program memory:
            .globl main             # label "main" must be global;
                                    # default trap.handler calls main.
            .globl label_a          #
        .globl label_b        #
        addi $26, $0, 8        # initialize the number of values inserting
        la $15, Array1        # pseudo-instruction: starting address of array
        add $17, $0, $0        # initialize i=0
                   # (1) PRINT A PROMPT
        addi    $2, $0, 4        # system call code for print_string
            la      $4, str_n        # pseudo-instruction: address of string
            syscall                  # print the str_n string
                   # (2) READ n
        addi    $2, $0, 5        # system call code for read int
            syscall                  # Read a line containing an integer
            add     $19, $2, $0     # copy returned int from $2 to tmp($19)
                   # (3)     print a  line
        add    $2, $0, 4
        la    $4, str_ln
                   # (4) PRINT n
        addi    $2, $0, 1
        add    $4, $0, $19
                   # (5) STORE INTEGER INTO ARRAY
        sw    $19, 16($10)      # STORES
        addi    $16, $16, 4     #move 4 bytes...
        j    label1
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Alpharetta, GA
    Rep Power
    You didn't mention the type of processor it is for, or the system it is running on (or simulating), but it looks like MIPS, probably running on the SPIM simulator, correct? This is important, as different types of CPUs can have radically different instruction sets (and even different assemblers for the same CPU type can have very different syntaces).

    In this case, the problem is not in the instruction, but in the contents of the $10 register (which is usually written as the $t2 register, BTW - all of the registers have standard names which reflect their conventional uses as well as numbers, which most assemblers, including the ones built into SPIM, MARS and MIPSsym recognize:
    $1                             $at - assembler temporary used by assembler in expanding pseudo-ops *
    $2-$3                       $v0/v1 - Stores the return value of a subroutine
    $4-$7                       $a0-$a3 - Argument registers for subroutine calls
    $8-$15,$24,$25     $t0-$t9  - temporary register
    $16-$23                  $s0-$s7 - saved register (local to a calling context, automatically saved on stack  
                                      when calling another subroutine) *
    $26-$27                  $k0-$k1 - reserved registers for kernel, see exception.S for details
    $28                        $gp - global pointer *
    $29                        $sp - stack pointer
    $30                        $fp/$s8 - normally the frame pointer but used as another saved register *
    $31                        $ra - the return address in a subroutine call  
    epc                        program counter
    * - registers you don't really need to care about
    (Table copied for a post which can be found here.)

    As I was saying... the problem is that you are using the $t2 register as the index of the store instruction, but you never set the value of $t2. This means that the index is reading 16 bytes past whatever garbage was in $t2 when the program started; it could be pointing almost anywhere, and chances are, it's not in a memory location which your program doesn't have permission to access. This causes a memory protection violation, as you've seen.

    What you need is to set the $10 register to point to the start of array1:
        la  $t2, array1
    If, for some reason, your assembler doesn't support the la pseudo-instruction, or your instructor doesn't want you to use it, you'll have to see how you can read the two halves of an address into a register manually; I can't think of any easy way to do it, as the assembler usually does the label address calculation automatically (hence the la macro in the first place), so the solution will probably be specific to a given assembler.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in ShortUnderstanding the C/C++ Preprocessor
    Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Rep Power
    Thank you very much, you gave me valuable information.
    Indeed, the processor is MIPS running on a SPIM simulator.

    I added this
    	addi	$10, $10, -4
    before sw $19, 0($10) # STORES

    and it worked. But this way I create a stack.
    My instructor hasn;t mentioned anything about la yet,
    but I will use it!

    Thank you again!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Rep Power

    Angry Try to solve your exercises by your own

    Please come to my office on Friday morning to explain you what i said on the previous lessons.
    your instructor!
  8. #5
  9. Type Cast Exception
    Devshed Supreme Being (6500+ posts)

    Join Date
    Apr 2004
    OAKLAND CA | Adam's Point (Fairyland)
    Rep Power
    If this is actually your course work you will note that the student in question took your class three years ago!!!!!

    “Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss

IMN logo majestic logo threadwatch logo seochat tools logo