我是汇编编程的新手。我正在尝试在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操作系统运行上述程序。
提示:当要打印零颗星(因此writestars
为0时),最后一行的r9
循环会发生什么?