13 Replies - 681 Views - Last Post: 22 October 2013 - 09:42 PM

#1 jaeger093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 13

Indexing in ARM Assembly

Posted 18 October 2013 - 01:16 AM

Hi I wanted to ask if there was a way to load an indexed integer to a register. To clarify, I have a bunch of integers stored in R5 and I'm loading all of them to R4. If R7 was 4 bits, can I load the stored integer that is location after 4 bits? What I have doesn't seem to be working.

MOV R2, #32

MOV R7, #4

MOV R8, #8

SUB R1,R1,#4

Loop:
LDMFA R5!,{R4}

ADD R1, R1, #4

CMP R2,R1

BNE Loop


LDR R6, [R4, R7]
LDR R0, [R4, R8]
CMP R6, R0 @What I'm trying to do is compare two integers that were stored in R5

Is This A Good Question/Topic? 0
  • +

Replies To: Indexing in ARM Assembly

#2 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Indexing in ARM Assembly

Posted 18 October 2013 - 02:05 AM

Quote

To clarify, I have a bunch of integers stored in R5 and I'm loading all of them to R4.

What do you mean? Bunch of integers in one integer register?

Quote

What I'm trying to do is compare two integers that were stored in R5

Are you trying to compare some bits of a register against some other bits in the same register?
Was This Post Helpful? 0
  • +
  • -

#3 jaeger093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 13

Re: Indexing in ARM Assembly

Posted 18 October 2013 - 03:35 AM

Sorry I can't explain this too well but what I'm trying to do is compare the contents stored in R5. So what I did was all the Integers that are stored in R5 are being loaded to R4. Now I'm trying to figure out a way to take individual integers in R4 and put them in R6 and R0 so I can compare them.

LDR R6, [R4, R7]
LDR R0, [R4, R8]
CMP R6, R0

These are the lines that are giving me trouble though. I want to take an integer from R4 that is referenced by R7 (my index) and move it to R6.


If the loaded contents in R4 are 45 72 3 8 and the integer 72 is located at #8, I made R7 #8 to say that I want that Integer to be loaded into R6. I hope that helped
Was This Post Helpful? 0
  • +
  • -

#4 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Indexing in ARM Assembly

Posted 18 October 2013 - 10:52 AM

View Postjaeger093, on 18 October 2013 - 01:35 PM, said:

Sorry I can't explain this too well but what I'm trying to do is compare the contents stored in R5. So what I did was all the Integers that are stored in R5 are being loaded to R4. Now I'm trying to figure out a way to take individual integers in R4 and put them in R6 and R0 so I can compare them.

LDR R6, [R4, R7]
LDR R0, [R4, R8]
CMP R6, R0

These are the lines that are giving me trouble though. I want to take an integer from R4 that is referenced by R7 (my index) and move it to R6.


If the loaded contents in R4 are 45 72 3 8 and the integer 72 is located at #8, I made R7 #8 to say that I want that Integer to be loaded into R6. I hope that helped


A register is supposed to contain ONE 32-bit integer only.
All the instructions expect that - even if you use byte-instructions. In that case the higher bits are jyst ignored. If you "pack" several 4-bit numbers into a register, it's your problem to handle them. The instruction set does not support that directly in any processor architecture that I can recall (Well BCD in some sense...).

You don't mean memory pointed to by a register?
You seem to be using indirect addressing: [R4, R8].

This post has been edited by turboscrew: 18 October 2013 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 jaeger093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 13

Re: Indexing in ARM Assembly

Posted 18 October 2013 - 12:26 PM

Ok thanks, I see why "LDR R6, [R4, R7]" isn't possible for what I'm trying to do. What I want to do is sort a random amount of integers from a text file and send them to another text file. If I stored all the integers into R5, what would be the best way to go about sorting them if I can't assign the integers to individual registers?
Was This Post Helpful? 0
  • +
  • -

#6 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Indexing in ARM Assembly

Posted 18 October 2013 - 02:38 PM

View Postjaeger093, on 18 October 2013 - 10:26 PM, said:

Ok thanks, I see why "LDR R6, [R4, R7]" isn't possible for what I'm trying to do. What I want to do is sort a random amount of integers from a text file and send them to another text file. If I stored all the integers into R5, what would be the best way to go about sorting them if I can't assign the integers to individual registers?


Basically, you read the data from the file into the memory,
sort it there, and writ the data into another file.

Depending on your sort algorithm and file size, you either read the whole file or handle the file block by block, which means temporary file and several passes.

In 32-bit integers are big enough, you read the numbers in and store them into memory as array of 32-bit integers. For the array(s) you need to reserve space.

You can access the array elements as "LDR R6, ARRAY1[R4], where the R4 acts as an index, and "ARRAY" is the base address of the array. To get to the next index you add 4 to R4.
(4 bytes = 32-bit integer)

The file reading, writing, opening and closing depends on your environment (system).
Was This Post Helpful? 0
  • +
  • -

#7 jaeger093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 13

Re: Indexing in ARM Assembly

Posted 18 October 2013 - 05:55 PM

This is the code I have for storing the integers to r4 from a file and then loading them to r8. Everything seems to be working fine but is the Array in the proper syntax? ARM sim is giving me a compile error.

.global start
.text
start:
   LDR R0,=InFileName 
   MOV R1,#0 
   swi SWI_Open 
   BCS InFileError 
   LDR R1,=InputFileHandle 
   STR R0,[R1] 
   LDR R0,=Num          
   LDR R0,[R1]
   LDR R4,=Num
   MOV R5, #0                     @R5 is index
   MOV R1, #0                     @R1 is size 
   
ReadLoop:
   LDR R0,=InputFileHandle
   LDR R0, [R0]
   swi SWI_RdInt
   BCS Next
   STR R0, [R4]
   ADD R4, R4, #4                @R4 is stored array[]                                
   ADD R1, R1, #4  
   B ReadLoop

Next:
   SUB R9, R9, #4
LoadLoop:
   LDMFA R4!,{R8}                 @R8 is loaded array[]
   ADD R9, R9, #4
   CMP R1,R9
   BNE LoadLoop
   
   LDR R3, Array[R5]              @ LDR R3, [R8,R5]
   

.data
InFileName: .asciz "numbers.txt"
Array:
.align
InputFileHandle: .skip 4
Num:
.end

Was This Post Helpful? 0
  • +
  • -

#8 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Indexing in ARM Assembly

Posted 19 October 2013 - 03:17 AM

My comments are there amongst the code.

.global start
.text
start:
   LDR R0,=InFileName 
   MOV R1,#0 
   swi SWI_Open 
   BCS InFileError 
   LDR R1,=InputFileHandle 
   STR R0,[R1] 

   LDR R0,=Num          What are these 2 lines for?
   LDR R0,[R1]

   LDR R4,=Num         R4 is pointer to the array

   MOV R5, #0                     @R5 is index
   MOV R1, #0                     @R1 is size 
   
ReadLoop:
   LDR R0,=InputFileHandle
   LDR R0, [R0]
   swi SWI_RdInt
   BCS Next
   STR R0, [R4]
   ADD R4, R4, #4                @R4 is stored array[]                                
   ADD R1, R1, #4     R1 is not cleared after using it in Open
   B ReadLoop

Next:
   SUB R9, R9, #4   What's in R9?:
LoadLoop: What is this loop for, and what's in R8? You already have the array: Num
   LDMFA R4!,{R8}                 @R8 is loaded array[]
   ADD R9, R9, #4
   CMP R1,R9
   BNE LoadLoop
    
   LDR R3, Array[R5]              @ LDR R3, [R8,R5]
   

.data
InFileName: .asciz "numbers.txt"
Array:
.align
InputFileHandle: .skip 4
  You need to allocate enough room for the array, or use dynamic allocation (swi 0x12, swi 0x13)
  I'd recommend static allocation of big enough space if possible
  like ".skip 1024" gives you room for 256 integers (1024 bytes, 4 bytes per integer).
Num:
.end



What did the compiler say? What was the error?

Funny that at least in this:
http://armsim.cs.uvi...rGuide4Plus.pdf
I didn't find how to tell id the input file was exhausted (end of file)

This post has been edited by turboscrew: 19 October 2013 - 03:21 AM

Was This Post Helpful? 0
  • +
  • -

#9 jaeger093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 13

Re: Indexing in ARM Assembly

Posted 19 October 2013 - 03:55 AM

.global start
.text
start:
   ldr r0,=InFileName 
   mov r1,#0 
   swi SWI_Open 
   bcs InFileError 
   ldr r1,=InputFileHandle 
   str r0,[r1] 
   
   ldr r0,=Num               Sorry I thought needed to also load Num to 
   ldr r0,[r1]               r0 for input      
   
   ldr r4,=Num                    @r4 is pointer to array
   mov r5, #0                     @r5 is index
   mov r1, #0                     @r1 is size 
   
ReadLoop:
   ldr r0,=InputFileHandle
   ldr r0, [r0]
   swi SWI_RdInt
   bcs Next
   str r0, [r4]                  
   add r4, r4, #4                @r4 is stored array[]                                
   add r1, r1, #4            I want to use r1 to keep track of the 
   b ReadLoop                total amount of integers that are being 
                             stored 
Next:
   sub r9, r9, #4
LoadLoop:
   ldmfa r4!,{r8}                 @r8 is loaded array[]
   add r9, r9, #4               This loop just loads the Integers
   cmp r1,r9                    that were stored in memory at r4
   bne LoadLoop                 to r8. Eventually I want to compare
                                integers to sort them, and I thought I needed to load them
   ldr r3, Array[r5]              @ ldr r3, [r8,r5]
   

.data
InFileName: .asciz "numbers.txt"
Array:
.align
InputFileHandle: .skip 4
Num: .skip 1024
.end   


The error I'm getting is "Syntax error, unexpected indent, expecting '[', or '='" for ldr r3, Array[r5]. I have more code written and can post it to help give some better context of what I'm thinking of doing. Sorry I'm still new to assembly language.
Was This Post Helpful? 0
  • +
  • -

#10 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Indexing in ARM Assembly

Posted 19 October 2013 - 06:51 AM

View Postjaeger093, on 19 October 2013 - 01:55 PM, said:

.global start
.text
start:
   ldr r0,=InFileName 
   mov r1,#0 
   swi SWI_Open 
   bcs InFileError 
   ldr r1,=InputFileHandle 
   str r0,[r1] 
   
   ldr r0,=Num               Sorry I thought needed to also load Num to 
   ldr r0,[r1]               r0 for input      
   
   ldr r4,=Num                    @r4 is pointer to array
   mov r5, #0                     @r5 is index
   mov r1, #0                     @r1 is size 
   
ReadLoop:
   ldr r0,=InputFileHandle
   ldr r0, [r0]
   swi SWI_RdInt
   bcs Next
   str r0, [r4]                  
   add r4, r4, #4                @r4 is stored array[]                                
   add r1, r1, #4            I want to use r1 to keep track of the 
   b ReadLoop                total amount of integers that are being 
                             stored 
Next:
   sub r9, r9, #4
LoadLoop:
   ldmfa r4!,{r8}                 @r8 is loaded array[]
   add r9, r9, #4               This loop just loads the Integers
   cmp r1,r9                    that were stored in memory at r4
   bne LoadLoop                 to r8. Eventually I want to compare
                                integers to sort them, and I thought I needed to load them
   ldr r3, Array[r5]              @ ldr r3, [r8,r5]
   

.data
InFileName: .asciz "numbers.txt"
Array:
.align
InputFileHandle: .skip 4
Num: .skip 1024
.end   


The error I'm getting is "Syntax error, unexpected indent, expecting '[', or '='" for ldr r3, Array[r5]. I have more code written and can post it to help give some better context of what I'm thinking of doing. Sorry I'm still new to assembly language.


It looks like '=' is some kind of immediate marker, so it should be before "Array".

ldr r3, =Array[r5]
If you need that for something.

It seems, though, that the offset indexed addressing is not supported.
The constant can only be one byte, and it's too small for the base address.
Maybe you need to load the address to a register and use it from there.

What you need is (I think)
LDR R5, =Array
and in the loop:
LDR R3, [R5], #4

This post has been edited by turboscrew: 19 October 2013 - 07:12 AM

Was This Post Helpful? 0
  • +
  • -

#11 jaeger093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 13

Re: Indexing in ARM Assembly

Posted 20 October 2013 - 12:38 AM

 .equ SWI_Open, 0x66  
   .equ SWI_PrInt,0x6b 
   .equ SWI_RdInt,0x6c  
   .equ SWI_Exit, 0x11 
.global Start
.text
Start:
   LDR R0, =InFile 
   MOV R1, #0 
   swi SWI_Open  
   LDR R1, =InputFileH
   STR R0, [R1] 
   MOV R4, #0 
   LDR R4, =Num                 
   MOV R1, #0
   MOV R9, #0
   MOV R5, #0
      
ReadLoop:
   LDR R0, =InputFileH
   LDR R0, [R0]
   swi SWI_RdInt
   bcs Next   
   STMEA R4!, {R0}                                            
   ADD R1, R1, #4  
   B ReadLoop

Next:
   ADD R9, R9, #4
   LDMFA R4!, {R5, R2}
   CMP R10, #4
   BGE Move
   MOV R6, R2
   B Loop
   
Move:
   MOV R6, R8
   
Loop:
   LDMFA R4!, {R3}                 
  
Compare:
   CMP R2, R3
   BGE Greater
   ADD R9, R9, #4
   CMP R1, R9
   BEQ End
   B Loop
   
Greater:
   CMP R6, R3
   BLT New
   MOV R7, R3
   MOV R2, R3               @ r7 is smallest number
   ADD R9, R9, #4
   CMP R1, R9
   BEQ End
   B Loop
   
New:
   ADD R9, R9, #4
   CMP R1, R9
   BEQ End
   B Loop

End:
   STMEA R7!, {R6}   
   STR R6, =Sp

   MOV R6, R7
   MOV R8, R6 
   ADD R10, R10, #4
   CMP R10, R1
   BNE Start  
   
StoreFile:  
   LDR R0, =OutFile 
   MOV R1, #0 
   swi SWI_Open                   
   LDR R0, =OutputFileH
   LDR R0, [R0]
   LDR R1, [R6]
   swi SWI_PrInt
   
Exit:
   swi SWI_Exit    
   
.data
InFile: .asciz "numbers.txt"
OutFile: .asciz "numsort.txt"
OutputFileH: .skip 4
Sp: .asciz " " 
.align
InputFileH: .skip 4
Num: .skip 1024
.end  


Thanks but unfortunately I couldn't get the array to work in ARM so I'm using a stack instead. I think it figured out the sorting loops but I'm a little confused on how to store the sorted integers to a file. Would the correct notation be something similar to what I have above?
Was This Post Helpful? 0
  • +
  • -

#12 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Indexing in ARM Assembly

Posted 20 October 2013 - 04:18 AM

Actually, your "Num" is already such array, and I understand it works fine?
Your output file is opened now for reading (R1 = 0)?

In writing, you set the file handle to R0 just like in reading, and put the integer to be written into R1 and swi SWI_PrInt.
Now you don't seem to loop through all the array.

You should also close the files. It may even be that the output is left in the file buffers and not written out to the disk at all if you dont't close the file.
Was This Post Helpful? 0
  • +
  • -

#13 jaeger093  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-October 13

Re: Indexing in ARM Assembly

Posted 22 October 2013 - 08:31 PM

Should I close the infile before I open the outfile?
Was This Post Helpful? 0
  • +
  • -

#14 turboscrew  Icon User is offline

  • D.I.C Addict

Reputation: 100
  • View blog
  • Posts: 615
  • Joined: 03-April 12

Re: Indexing in ARM Assembly

Posted 22 October 2013 - 09:42 PM

It does not matter. You can close both files after the deed is done - just before exit, but "nice way" is to do it right after it's not needed anymore.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1