为什么我遇到分段错误?

Why I got a segmentation fault?

我是汇编编程的新手。我正在尝试在 x64 程序集中制作一个简单的程序来打印一个倒三角形到控制台。我希望我的程序输出如下内容:

********
 *******
  ******
   *****
    ****
     ***
      **
       *

这是我到目前为止的努力:

;--------------------------------------
; compile and run:
; $ nasm -f elf64 invtriangle.asm
; $ ld -s -o invtriangle invtriangle.o
; $ ./invtriangle
;--------------------------------------

                bits 64
                global _start

                section .text
_start:
                mov     r8, 0          ; copy maximum number of whitespace characters allowed for first line to register 'r8'
                mov     r9, 8          ; copy maximum number of stars allowed for first line to register 'r9'
                mov     r10, 0         ; number of whitespace characters written on line so far
                mov     r11, 0         ; number of star characters written on line so far
                mov     rbx, output    ; copy address of the string pointer to register 'rbx'
writeline:
writewhitespace:
                cmp     r8, 0          ; check if the value of register 'r8' is zero
                je      writestars     ; if so, skip writing whitespaces
                mov     byte[rbx], ' ' ; write a whitespace character
                inc     rbx            ; advance pointer to next address to write
                inc     r10            ; increment number of whitespace characters written on line so far by 1
                cmp     r10, r8        ; compare register values: check if we reached the maximum number of whitespace characters allowed for the current line
                jne     writewhitespace; if not, continue writing whitespaces
writestars:
                mov     byte[rbx], '*' ; write s star
                inc     rbx            ; advance pointer to next address to write
                inc     r11            ; increment number of star characters written on line so far by 1
                cmp     r11, r9        ; compare register values: check if we reached the maximum number of star characters allowed for the current line
                jne     writestars     ; if not, continue writing stars
lineend:
                mov     byte[rbx], 10  ; write a new line character (ascii value for new-line is 10)
                inc     rbx            ; advance pointer to next address to write
                inc     r8             ; the next line will be one whitespace longer
                dec     r9             ; the next line will be one star shorter
                mov     r10, 0         ; reset the counter (number of whitespace characters written on line)
                mov     r11, 0         ; reset the counter (number of star characters written on line)
                cmp     r8, maxlines   ; did we exceed the maximum number of lines?
                jng     writeline      ; if not, continue writing lines
                mov     rax, 1         ; system call for write (64 bit)
                mov     rdi, 1         ; file descriptor = stdout
                mov     rsi, output    ; copy the address of string 'output' to output
                mov     rdx, nbytes    ; copy number of bytes in output
                syscall                ; invoke OS to write
                mov     rax, 60        ; system call for exit (64 bit)
                mov     rdx, 0         ; exit status = 0
                syscall                ; invoke OS to exit

                section .bss
maxlines        equ     8              ; maximum number of lines to write
nbytes          equ     72             ; (8 + 8 + 8 + 8 + 8 + 8 + 8 + 8) + 8 = 72 bytes (don't forgot to take account of new-line characters!)
output          resb    nbytes         ; initialize a string pointer by reserving number of bytes

当我运行上面的汇编程序时,我一直遇到段错误,我无法找出它的原因。我试图更改寄存器,但得到了相同的结果。有趣的是,当我在寄存器 r9 中将第一行允许的最大星数从 8 增加到 10 时,程序 运行 没有分段错误但输出不是我预期的(我不知道为什么会这样)。我不确定我是否在 bss 部分正确计算了变量 nbytes 。我将不胜感激任何形式的帮助,谢谢!

我正在使用 64 位 Ubuntu OS 到 运行 上面的程序。

提示:如果要打印零星(因此 r9 为 0),最后一行的 writestars 循环会发生什么情况?