简体   繁体   中英

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. 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. As Peter points out, there's esi and edi still available.

_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. 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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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