How can I print out a 2D array in ARM assembly?
I’m trying to take each row and then turn it into a null terminated string but it only prints out the first number. How can I move to the next number?
I was told this:To print the array it’s best to write a subroutine that loops through the array printing it one element at a time. printf only takes one argument and as in assembly language an array is just a load of numbers printf will only print the first one. Alternately you could write a subroutine that makes the board into a null terminated string. In either case the subroutine can both print the board and format it nicely.
But I only understand how to do it this way:
AREA Countdown, CODE, READONLY
IMPORT main
IMPORT getkey
IMPORT sendchar
IMPORT printf
EXPORT start
start
LDR R3, = teststr
LDR R4, = array
wh1 LDRB R0, [R4]
CMP R0, #0
BEQ stop
ADD R0, R0, #0X30
STRB R0, [R3]
ADD R3, R3, #1
ADD R4, R4, #1
B wh1
stop B stop
AREA Strings, DATA, READWRITE
teststr DCB "",0
array DCD 6, 3, 8, 2, 5, 2, 9, 1
DCD 3, 7, 2, 8, 5, 7, 2, 6
DCD 2, 4, 7, 4, 2, 6, 7, 4
DCD 1, 9, 3, 2, 9, 5, 6, 8
DCD 7, 5, 3, 7, 5, 8, 2, 1
END
Your program has several problems associated with it. One is that you’re not reserving space for the destination string, so you will be writing on unallocated memory. The other is that you’re not outputting a newline character.