【发布时间】:2015-04-12 07:15:49
【问题描述】:
我正在使用 NASM 编译我的 ASM 程序,但我无法弄清楚如何使用循环在一行上打印整个数组(不一定知道数组有多大)。每当我使用 printf 创建一个循环时,它都会在多行而不是一行上打印值。知道如何使 printf 使用循环在单行上打印数组的多个值吗?我得到值 1-9 但都在不同的行而不是同一行。这是在不使用外部库的情况下完成的,除了:printf c 库。非常感激任何的帮助。我的代码如下。
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 我同意你的看法。 Ikegami 感谢您的帮助,尽管当我要求汇编程序的一些语法时,这就是我所寻求的,因为在像 c++ 这样的另一种语言中,我知道如何在使用 cout 之类的东西时创建新行或不创建新行,但是对于汇编程序,语法有点不同,这就是我要问的,因为我已经知道我在寻找什么(不添加 \n)但我问的是如何不添加 \n,因为我没有不知道放在哪里,反正我找到了我正在寻找的答案。感谢大家的帮助。
-
相关:如果您根本不打印换行符,您可能需要 fflush:Printf without newline in assembly
标签: arrays assembly syntax printf nasm