February 25th, 2009, 03:06 AM
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
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...
February 25th, 2009, 04:04 AM
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:
(Table copied for a post which can be found here.)
$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
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:
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.
February 25th, 2009, 04:31 AM
Thank you very much, you gave me valuable information.
Indeed, the processor is MIPS running on a SPIM simulator.
I added this
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!
February 29th, 2012, 05:06 PM
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.
February 29th, 2012, 05:47 PM
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