简体   繁体   English

尝试以汇编语言翻转三角形

[英]Trying to flip a triangle in assembly language

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

As suggested by @Jester, counting up is the solution, but only in one loop (I made the little change in your code) : 正如@Jester所建议的那样,增加计数是一种解决方案,但只能在一个循环中进行(我对您的代码进行了很小的更改):

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 : 两个三角形 :只需在空格前后调用“ Proc1”就可以显示两个三角形,非常重要的是,空格计数器“ EBP”必须为12(用于适当分隔),而在过程“空格”中则为EBP必须减少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

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

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

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