0

I'm trying to read integers from an input.txt file, below is my read loop where I'm attempting to read and store the integers into an array. I keep getting "Access to unaligned memory location, bad address=ffffff" on any line after the line with "LDR R2, [R2,R5,LSL #2]...im using ARM SIM. Does anyone know what I'm doing wrong?

    start:
    MOV R5, #0      @int i 
    MOV R1, #0
    swi SWI_Open
    LDR R1,=InFileH
    STR R0,[R1]
    MOV R3, #0

readloop:
    LDR R0, =InFileH
    LDR R0, [R0]
    swi SWI_RdInt
    CMP R0, #0
    BEQ readdone
    @the int is now in R0
    MOV R1, R0
    LDR R3,=a
    STR R2,[R3,R5,LSR#2]
    MOV R2, R1
    ADD R5, R5, #1      @i++
    bal readloop

readdone:
    MOV R0, #0
    swi SWI_Close
    swi SWI_Exit

.data
.align 4
InFileH:    .skip 4
InFile:     .asciz  "numbers.txt"
OutFile:    .asciz "numsort.txt"
OutFileH:   .skip 4
NewLine:    .asciz "\n"

a:  .skip 400
  • You don't set R2 to a value before you use it in the source operand in `LDR R2,[R2,R5,LSL #2]` – Ross Ridge Apr 21 '17 at 03:43
  • How would I write that line in order to save the offset and store an integer its reading to the correct address in the array. – Brendon Apr 21 '17 at 03:52
  • Two hints: LDR loads a value from memory into a register. STR stores a value from a register into a memory. You load the address of the array into R3 with the `LDR R3,=a` instruction, but you don't use the value in R3 anywhere. – Ross Ridge Apr 21 '17 at 04:04
  • I changed it from LDR to STR and placed R3 inside. Im geting confused because all the places I look for correct syntax state assembly as "LSL R2, R3, #2." I put what I have so far above. Is that the right track at least? – Brendon Apr 21 '17 at 04:31
  • 2
    `[R3,R5,LSR#2]` <-- I think you want `LSL`, not `LSR`. – Michael Apr 21 '17 at 06:29

1 Answers1

0

i had faced similar issue while programming arm assembly
this was because it was expecting offset in multiples of 4

STR R2, [R1, #2]

the above instruction throws the similar error. so it was resolved by using

STR R2, [R1, #4]

for better understanding clickhere

Abhishek D K
  • 2,257
  • 20
  • 28