为什么下面的汇编代码没有打印出新行 (0xa) 字符?

ilg*_*aar 2 macos assembly x86-64 nasm

在下面的代码中,NASM 按预期运行所有内容,除了在末尾打印一个换行符。可能是什么原因?

%define sys_write       0x2000004           ; system call number for write
%define sys_read        0x2000003           ; system call number for read
%define sys_exit        0x2000001           ; system call number for exit
%define std_out         0x1                 ; file descriptor number for standard output
%define std_in          0x2                 ; file descriptor number for standard input
%define new_line        0xa                 ; a new line character
%define current_address $                   ; address of current location

; a macro to implement the write system call and write
; a string into standard output using two parameters
%macro write_string 2
    mov rax, sys_write                          ; system call to write
    mov rdi, std_out                            ; standard output
    mov rsi, %1                                 ; a string to write
    mov rdx, %2                                 ; length of the string
    syscall                                     ; kernel call interruption
%endmacro

; a macro to implement the exit system call
%macro exit 0
    mov rax, sys_exit                           ; system call to exit
    syscall
%endmacro

    section .data

message:    db 'Displaying 9 stars', new_line       ; a message
.length:    equ current_address - message           ; length of the message
asterisks:  times 9 db '*'                          ; a string of 9 asterisks

global start

    section .text

start:                                      ; linker entry point

    write_string message, message.length    ; print a message to standard output
    write_string asterisks, 9               ; print 9 asterisks
    write_string new_line, 1                ; print a line feed(new line)

    exit                                    ; end the program
Run Code Online (Sandbox Code Playgroud)

组装后:

user$ nasm -f macho64 9stars.asm
user$ ld 9stars.o -o 9stars
ld: warning: -macosx_version_min not specified, assuming 10.10
user$ ./9stars
Run Code Online (Sandbox Code Playgroud)

预期的输出应该是:

Displaying 9 stars
*********
user$
Run Code Online (Sandbox Code Playgroud)

但目前的输出是:

Displaying 9 stars
*********user$
Run Code Online (Sandbox Code Playgroud)

它看起来像write_string new_line, 1没有把new_line虽然作为一个字符常量db 'Displaying 9 stars', new_linenew_line增加了一个新的行字符,字符代码10。我最初的猜测是,new_line是不是所有的字符常量。这可能是什么原因?这怎么能解决?我在 Mac OS X 10.10 上。我有一个核心 i5 m540 Intel CPU。我使用 NASM 版本 2.11.06。

Pau*_*l R 5

write_string需要字符串的地址,但new_line只是字符值 0xa。试图将其解释为字符串的地址将产生不可预测的结果。如果你想要一个只包含一个0xa可以打印的字符串,那么你需要做这样的事情:

    section .data

message:    db 'Displaying 9 stars', new_line       ; a message
.length:    equ current_address - message           ; length of the message
asterisks:  times 9 db '*'                          ; a string of 9 asterisks
newline:    db new_line                             ; <<< a single LF

global start

    section .text

start:                                      ; linker entry point

    write_string message, message.length    ; print a message to standard output
    write_string asterisks, 9               ; print 9 asterisks
    write_string newline, 1                 ; <<< print LF
Run Code Online (Sandbox Code Playgroud)

  • 实际上,它非常可预测——0xa 不会是有效地址,因此系统调用将失败并显示 EFAULT 并且不打印任何内容。由于他不检查系统调用中的错误,因此它什么也不做。 (3认同)