0

So, recent I have been taking assembly classes and one of them gave me an assignment. I coded everything except the last part... I just have to add the numbers in a given array and print the sum... easy right? No, i have an error when i run it says that line 12 is out of range for some reason =/

So, here is my code:

.data

array: .word 1,2
size: .word 2
la $s0, array
lw $s1, size
li $t9, 0

.text

loop:
lw $a0, ($s0)
add $a0, $a0, $a0
addi $s0, $s0, 4
add $t9, $t9, 1
blt $t9, $s1, loop

li $v0, 1
syscall
2
  • which line is line 12? lw $a0, ($s0)? What exactly is the error message? And what do you see when you run this in a debugger and look at register values? Commented Sep 24, 2017 at 17:01
  • @peter Cordes Error in C:\Users\Mohamad\Desktop\addingarray.asm line 13: Runtime exception at 0x00400004: address out of range 0x00000000 Commented Sep 24, 2017 at 17:23

1 Answer 1

2

la $s0, array is in the .data section, so it never executes.

If you'd used a debugger to single-step, you would have seen that your program never executes the instructions in the data section, and presumably starts execution at loop:.

At that point, $s0 happens to still be zero (according to your error message). Anyway, it's not pointing at array.

Sign up to request clarification or add additional context in comments.

7 Comments

yes that worked great and i even noticed some logic errors and it now runs great thanks. 1 more thing... if i want to compare to strings do i just use beq bne? or is there more to it? im having some problems with my other program
i just wanna see if a word is in the array here is my code idk whats wrong tbh .data array: .asciiz "one", "two", "three" word: .asciiz "nine" msgone: .asciiz "\nThe word is found" msgtwo: .asciiz"\nThe word was not found" size: .word 4 .text la $s0, array lw $s1, size la $a1, word li $t9, 0 loop: beq $t9, $s1 , fail la $a0, ($s0) addi $s0, $s0, 4 add $t9,$t9, 1 beq $a1, $a0, found bne $a1, $a0, loop fail: la $a0, msgtwo li $v0, 4 syscall found: la $a0, msgone li $v0, 4 syscall j done done:
@MohammadAlBaba: If you entire string fits in a 32-bit register, yeah you could compare it with beq. Otherwise not. google for how to implement strcmp in asm. use a debugger. If you're using SPIM or MARS, there's probably one built-in.
i am using mars but my program always says that the string isnt contained in the array its making me a bit insane :P
@MohammadAlBaba: So single-step your code and look at what values are in registers. Note that .asciiz "one" is 4 bytes, but .asciiz "three" is 6 bytes long. So your "array" isn't an array anymore. It's just some concatenated data with separately-labeled elements, separated by 0 bytes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.