[英]Trying to print a star triangle in Assembly
section .data
star db '*'
num db '1'
endl db 10
line db '1'
section .text
global _start
_start:
Star:
mov edx,1 ;using 1 byte as appropriate
mov ecx,star ;;moving num into ecx to print the star
mov ebx,1 ;;_STDOUT
mov eax,4 ;;SYS_WRITE
int 80h
inc byte [num];num= 2
mov al, [line];al=1
mov bl, [num];bl=1
cmp al,bl
je Star;always false
jmp PrintLine
;loop
PrintLine:
mov edx,1;using 1 byte as appropriate
mov ecx,endl ;;moving num into ecx to print the star
mov ebx,1 ;;_STDOUT
mov eax,4 ;;SYS_WRITE
int 80h
inc byte [line] ;2
cmp byte[line] , '9' ;comparing 2 to 9
jl Star
end:
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h;
此代碼的結果只是 9 行中的單個星,但我不知道如何隨着行數的增加而增加星數。 請幫忙。 我使用了兩個循環,其中一個循環在另一個循環內。 如果跳轉失敗或通過,則增加。 一個循環用於打印星星,另一個循環用於打印下一行。 我已經寫了很多次邏輯,從邏輯上講它似乎有效,但我無法弄清楚代碼的語法和位置
我喜歡把每個部分分解成步驟。 首先,我不會使用 memory 作為變量。 正如彼得指出的那樣, esi
和edi
仍然可用。
_start:
mov esi, 0 ; line counter
mov edi, 0 ; star counter
主循環任務基本上是檢查我們是否已經達到 9 行,如果是則退出。 如果不是,我們需要打印一些星星:
main_loop:
inc esi
cmp esi, 9
jg end ; have we hit 9 lines?
; print 1 whole line of stars
call print_line
jmp main_loop
現在我們需要實際打印一行星星:
print_line:
mov edi, 0; we've printed no stars yet, this is a new line
printline_loop:
call print_star ; print a single star character
inc edi ; increment the number of stars we've printed
; have we printed the same number of stars as we have lines?
cmp edi, esi
jne printline_loop
call print_eol
ret
最后,打印星號或換行符的最后一組單獨的子例程:
print_star:
mov edx, 1
mov ecx, star
mov ebx, 1
mov eax, 4
int 80h
ret
print_eol:
mov edx, 1
mov ecx, endl
mov ebx, 1
mov eax, 4
int 80h
ret
end:
mov eax, 1
mov ebx, 0
int 80h
Output:
*
**
***
****
*****
******
*******
********
*********
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.