Trying to flip a triangle in assembly language

2019-09-21 10:15发布

INCLUDE Irvine32.inc
.code
main PROC

        mov ecx, 6 
L1:
        call Proc1
        call CRLF
        loop L1

        exit
main ENDP

proc1 PROC  USES ecx
        mov  al, 'A'
L2:     call WriteChar
        inc al
        loop L2
        ret
proc1 ENDP

END main

The output of my code is

ABCDEF
ABCDE
ABCD
ABC
AB
A

In a triangle form going down but I need to flip it to where it is

A
AB
ABC
ABCD
ABCDE
ABCDEF

Edit:

Trying to mirror the right triangle.

A            A
AB          BA
ABC        CBA
ABCD      DCBA
ABCDE    EDCBA
ABCDEF  FEDCBA

2条回答
2楼-- · 2019-09-21 10:33

As suggested by @Jester, counting up is the solution, but only in one loop (I made the little change in your code) :

INCLUDE Irvine32.inc
.code
main PROC

        mov ecx, 1     ;<=============================
L1:
        call Proc1
        call CRLF
;       loop L1        ;<============================= NO MORE LOOP HERE.
        inc  ecx       ;<============================= CX++.
        cmp  ecx, 6    ;<============================= 
        jbe  L1        ;<============================= IF (CX <= 6) REPEAT.

        exit
main ENDP

proc1 PROC  USES ecx
        mov  al, 'A'
L2:     call WriteChar
        inc al
        loop L2
        ret
proc1 ENDP

END main

The output now is :

A
AB
ABC
ABCD
ABCDE
ABCDEF

Now the mirror : the idea is to print blank spaces before the letters:

INCLUDE Irvine32.inc
.code
main PROC

        mov ecx, 1     ;<=============================
        mov ebp, 6     ;EBP IS USED AS REVERSE COUNTER FOR BLANK SPACES.

L1:     call spaces    ;</////////////////////////////
        call Proc1
        call CRLF
;       loop L1        ;<============================= NO MORE LOOP HERE.
        inc  ecx       ;<============================= CX++.
        cmp  ecx, 6    ;<============================= 
        jbe  L1        ;<============================= IF (CX <= 6) REPEAT.

        exit
main ENDP

proc1 PROC  USES ecx
        mov  al, 'A'
L2:     call WriteChar
        inc al
        loop L2
        ret
proc1 ENDP

spaces PROC              ;</////////////////////////////
        push ebp         ;PRESERVE CURRENT SPACES COUNTER.
        mov  al, ' '     ;SPACE TO PRINT.
L3:     call WriteChar   ;PRINT SPACE.
        dec  ebp         ;DECREASE COUNTER.
        jnz  L3          ;IF COUNTER > 0 REPEAT.
        pop  ebp         ;RESTORE SPACES COUNTER.
        dec  ebp         ;DECREASE ONE SPACE FOR THE NEXT LINE.
        ret              ;</////////////////////////////
spaces ENDP              ;</////////////////////////////

END main

TWO TRIANGLES : two triangles can be displayed just by calling "Proc1" before and after the spaces, and, very important, the spaces counter, "EBP", must be 12 (for appropiate separation) and, in proc "Spaces", EBP must decrease by 2 :

INCLUDE Irvine32.inc
.code
main PROC

        mov ecx, 1     ;<=============================
        mov ebp, 12     ;EBP IS USED AS REVERSE COUNTER FOR BLANK SPACES.

L1:     call Proc1     ;LEFT TRIANGLE ! ! !
        call spaces    ;</////////////////////////////
        call Proc1     ;RIGHT TRIANGLE ! ! !
        call CRLF
;       loop L1        ;<============================= NO MORE LOOP HERE.
        inc  ecx       ;<============================= CX++.
        cmp  ecx, 6    ;<============================= 
        jbe  L1        ;<============================= IF (CX <= 6) REPEAT.

        exit
main ENDP

proc1 PROC  USES ecx
        mov  al, 'A'
L2:     call WriteChar
        inc al
        loop L2
        ret
proc1 ENDP

spaces PROC              ;</////////////////////////////
        push ebp         ;PRESERVE CURRENT SPACES COUNTER.
        mov  al, ' '     ;SPACE TO PRINT.
L3:     call WriteChar   ;PRINT SPACE.
        dec  ebp         ;DECREASE COUNTER.
        jnz  L3          ;IF COUNTER > 0 REPEAT.
        pop  ebp         ;RESTORE SPACES COUNTER.
        sub  ebp, 2      ;DECREASE 2 SPACES FOR THE NEXT LINE.
        ret              ;</////////////////////////////
spaces ENDP              ;</////////////////////////////

END main
查看更多
走好不送
3楼-- · 2019-09-21 10:38

Alternate solution:

proc1 PROC  USES ecx
        neg ecx
        add ecx, 7   ; print 7-ecx letters
        mov  al, 'A'
L2:     call WriteChar
        inc al
        loop L2
        ret
proc1 ENDP
查看更多
登录 后发表回答