简体   繁体   English

试图在大会中打印一个星形三角形

[英]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;

the result for this code is just single stars in 9 rows but i cannot figure how to increment number of stars as the number row increases.此代码的结果只是 9 行中的单个星,但我不知道如何随着行数的增加而增加星数。 PLEASE HELP.请帮忙。 I used Two loops where one loops is inside the other.我使用了两个循环,其中一个循环在另一个循环内。 And increment if a jump fails or passes.如果跳转失败或通过,则增加。 One loop is used to print stars and other is supposedly used to print the next line.一个循环用于打印星星,另一个循环用于打印下一行。 I have written out the logic a bunch of times and logically it seems to work but i cant figure out the syntax and the placement of the code我已经写了很多次逻辑,从逻辑上讲它似乎有效,但我无法弄清楚代码的语法和位置

I like to break each part in to steps.我喜欢把每个部分分解成步骤。 To begin with, I wouldn't use memory for the variables.首先,我不会使用 memory 作为变量。 As Peter points out, there's esi and edi still available.正如彼得指出的那样, esiedi仍然可用。

_start:

    mov esi, 0 ; line counter
    mov edi, 0 ; star counter

The main loops task is basically to check if we've reached 9 lines and exit if so.主循环任务基本上是检查我们是否已经达到 9 行,如果是则退出。 If not though, we need to print some stars:如果不是,我们需要打印一些星星:

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

Now we need to actually print a line of stars:现在我们需要实际打印一行星星:

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

To finish up, the final set of individual subroutines to print a star or a newline character:最后,打印星号或换行符的最后一组单独的子例程:

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

Here it is running at IDEOne它在 IDEOne 运行

Output: Output:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM