试图用汇编语言翻转三角形

时间:2016-05-09 20:27:34

标签: assembly x86 masm irvine32

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

我的代码输出是

ABCDEF
ABCDE
ABCD
ABC
AB
A

以三角形形式向下但我需要将其翻转到它所在的位置

A
AB
ABC
ABCD
ABCDE
ABCDEF

编辑:

尝试镜像直角三角形。

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

2 个答案:

答案 0 :(得分:1)

正如@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

现在的输出是:

A
AB
ABC
ABCD
ABCDE
ABCDEF

现在镜像:想法是在字母前打印空格:

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

两个三角形:只需调用&#34; Proc1&#34;在空间之前和之后,非常重要的是,空间计数器,&#34; EBP&#34;,必须是12(适用于合适的分离),并且在proc&#34; Spaces&#34;中,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

答案 1 :(得分:1)

替代解决方案:

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