我正在使用NASM編譯我的ASM程序,並且在解決如何在單行上打印整個數組時遇到困難(不一定知道數組有多大)使用循環。每當我用printf創建一個循環時,它都會在多行而不是一行上打印這些值。任何想法如何讓printf使用循環在單行上打印數組的多個值?我得到值1-9,但全部在不同的線上,而不是相同的線。這可以在不使用以外的外部庫的情況下完成:printf c庫。非常感激任何的幫助。我的代碼如下。使用printf asm nasm打印整行數組在單行上
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
@JonathonReinhart我同意你的意見。池上感謝您的幫助,雖然當我要求彙編程序的一些語法時,這正是我所尋求的,因爲在另一種語言如C++中,我知道如何在使用類似cout的東西時創建新行或不創建新行,但是對於彙編程序來說,語法有點不同,這正是我所問的,因爲我已經知道我在找什麼(不添加\ n),但是我問如何不添加\ n,因爲我沒有不知道放哪裏,反正我找到了我正在尋找的答案。感謝所有的幫助。 – Rex 2015-02-11 19:28:45