I am using NASM for compiling my ASM program and I am having trouble figuring out how to print an entire array on a single line (without necessarily knowing how big the array is) using a loop. Whenever I create a loop with printf it prints the values on multiple lines instead of one line. Any idea how to make printf print multiple values of an array on a single line using a loop? I get values 1-9 but all on a different line instead of the same line. This is to be done without using external libraries other than: the printf c library. Any help would be most appreciated. The code I have is below.
extern printf
SECTION .data ; Data section, initialized variables
array: dd 1, 2, 3, 4, 5, 6, 7, 8, 9, 0; this is a test array for testing purposes
arrayLen: dd 9 ; length of array
aoutput: db "%d", 10, 0 ; output format
SECTION .text ; Code section.
global main ; the standard gcc entry point
main: ; the program label for the entry point
push ebp ; set up stack frame
mov ebp,esp
mov ecx, [arrayLen] ; loop counter set up
mov esi, 0 ; counter to increment set up for looping through array
.loop:
push ecx ; make sure to put ecx (counter) on stack so we don't lose it when calling printf)
push dword [array + esi] ; put the value of the array at this (esi) index on the stack to be used by printf
push dword aoutput ; put the array output format on the stack for printf to use
call printf ; call the printf command
add esp, 8 ; add 4 bytes * 2
pop ecx ; get ecx back
add esi, 4
loop .loop
mov esp, ebp ; takedown stack frame
pop ebp ; same as "leave" op
mov eax,0 ; normal, no error, return value
ret ; return
The only thing that determines whether something is printed on a single line or multiple, is if you print a newline character ( \\n
) or not.
Here, when you say 10
, that is the ASCII value for a Line Feed. If you change this:
aoutput: db "%d", 10, 0
to this:
aoutput: db "%d ", 0
your values will be separated by spaces, instead of by new lines.
After the final value in the sequence, you can print a lone newline character:
push 0x0A ; New line character
call putchar
I figured out this (which is the same as the other answer, I just found it out before I saw it was posted).
I figured it out, I changed the format definition to:
aoutput: db "%d ", 0 ; output format
from:
aoutput: db "%d ", 10, 0 ; output format
and just added an newline format to add a new line at the end.
newline: db "", 10, 0 ; newline format
The problem is that you are outputting a line feeds after each element.
What follows are two solution. Neither of them suffer from the problem of leaving a trailing blank space as the two existing solutions do.
Note that I made a slight rearrangement of the loop to allow empty arrays. Allowing empty arrays is a good practice even if it's not necessary since it doesn't require any extra instructions.
Solution 1: Use "%d\\n"
as the format if it's the last element or "%d "
otherwise.
This is good if you don't want to print the line feed for empty arrays.
format1: db "%d ", 0
format2: db "%d", 10, 0
jmp .loop3
.loop1:
mov eax,format1
cmp ecx,1
jnz .loop2
mov eax,format2
.loop2
push ecx
push dword [array + esi]
push dword eax
call printf
add esp, 8
pop ecx
add esi, 4
.loop3:
loop .loop1
Solution 2: Use "%d"
as the format if it's the first element. Use " %d"
as the format otherwise. Print a line feed after the loop.
This is good if you want to print the line feed even for empty arrays.
format1: db "%d", 0
format2: db " %d", 0
mov eax, format1
jmp .loop2
.loop1:
push ecx
push dword [array + esi]
push dword eax
call printf
add esp, 8
pop ecx
add esi, 4
mov eax, format2
.loop2:
loop .loop1
push 10
call putchar
Pardon any errors; the x86 didn't have an eax
register the last time I wrote assembly for it. The logic should be evident in any case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.