2015-02-11 40 views
0

我正在使用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 
+0

@JonathonReinhart我同意你的意見。池上感謝您的幫助,雖然當我要求彙編程序的一些語法時,這正是我所尋求的,因爲在另一種語言如C++中,我知道如何在使用類似cout的東西時創建新行或不創建新行,但是對於彙編程序來說,語法有點不同,這正是我所問的,因爲我已經知道我在找什麼(不添加\ n),但是我問如何不添加\ n,因爲我沒有不知道放哪裏,反正我找到了我正在尋找的答案。感謝所有的幫助。 – Rex 2015-02-11 19:28:45

回答

1

決定是否東西上印有一行或多唯一的一點,就是如果你打印一個換行符(\n)與否。

這裏,當您說10時,這是換行的ASCII值。如果更改此:

aoutput: db "%d", 10, 0 

這樣:

aoutput: db "%d ", 0 

您的值將用空格隔開,而不是由新線。

後的序列中的最終值,你可以打印一個孤獨的換行符:

push 0x0A  ; New line character 
call putchar 
+0

請注意您的解決方案留下了尾隨空間。這可能很煩人,並可能導致屏幕上出現空白行。 – ikegami 2015-02-11 21:21:33

0

我想通了這一點(這是另一個相同的答案,我只是發現它之前,我看到了它被張貼)。

我想通了,我改變了格式定義:從

aoutput: db "%d ", 0 ; output format 

aoutput: db "%d ", 10, 0 ; output format 

,只是增加了一個新行格式在末尾添加一個新行。

newline: db "", 10, 0 ; newline format 
+0

FWIW,Nasm會識別「\ n」和其他轉義序列,如果您將您的字符串放在「反引號」(我們在此用來表示代碼的字符)中 - 而不是單引號或雙引號。 – 2015-02-11 21:59:57

0

問題是,您正在每個元素之後輸出換行符。

以下是兩種解決方案。兩個現有的解決方案都沒有留下留下空白空間的問題。

請注意,我對循環進行了輕微的重新排列以允許空數組。即使沒有必要,允許空陣列也是一種很好的做法,因爲它不需要任何額外的指令。


解決方法1:使用"%d\n"作爲格式,如果它是最後一個元素或"%d "其他。

如果您不想打印空數組的換行符,這很好。

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 

解決方案2:使用"%d",彷彿這是第一個元素的格式。否則使用" %d"作爲格式。在循環後打印換行符。

如果您要打印換行即使是空數組,也是如此。

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 

赦免任何錯誤;上次我爲它編寫程序集時,x86沒有註冊eax。無論如何,邏輯應該是明顯的。