8086汇编中的星号金字塔星号、金字塔

2023-09-03 11:20:40 作者:世上最温暖的两个字是晚安

%1

我应该在8086汇编语言中做这个练习

    *
   ***
  *****
 *******
*********
但作为输出,我只得到第一个星号。问题出在哪里?

在代码段中安排自己定义的栈空间

他们告诉我用POP和PUSH,但没有学习过,我不知道怎么做。我希望有人能给我一个有效的帮助来解决这个代码。我还在DOSBOX上编写代码,这是我的代码:

.MODEL SMALL
.STACK
.DATA
nl db 0dh,0ah, '$'
. CODE
mov ax,@data
mov ds,ax
mov cx,5
mov bx,1

for1: 
push cx
mov dl,20h
mov ah,2
for2:
int 21h
loop for2
mov cx,bx
mov dl,'*'
mov ah,2
for3:
int 21h
loop for3
lea dx,nl
mov ah,9
int 21h
inc bx
inc dx
inc cx

loop for1

mov ah,4ch
int 21h

END

这是我们用TASM开发的一个学校问题,我们还没有研究过复杂的指令,只研究了我编写的代码中使用的那些指令。 有人能改正吗?

推荐答案

问题出在哪里?

您在多个方面使用CX寄存器错误。

查看所需的棱锥体:

     *       5 leading spaces and 1 asterisk
    ***      4 leading spaces and 3 asterisks
   *****     3 leading spaces and 5 asterisks
  *******    2 leading spaces and 7 asterisks
 *********   1 leading space  and 9 asterisks

您的代码已经尝试做的是在BX寄存器中保持星号的连续计数。更正拼写错误inc dx并将其更改为inc bx后,就可以了。

在控制前导空格的数量时,您对CX寄存器的选择使问题复杂化,以至于程序陷入停滞。 你应该为这个目的选择一个额外的收银机。我建议选择BP

    mov     bp, 5       ; first line has 5 spaces
    mov     bx, 1       ; first line has 1 asterisk
next: 

    mov     cx, bp      ; current number of spaces
spaces:
    mov     dl, " "
    mov     ah, 02h
    int     21h
    loop    spaces

    mov     cx, bx      ; current number of asterisks
asterisks:
    mov     dl, "*"
    mov     ah, 02h
    int     21h
    loop    asterisks

    lea     dx, nl
    mov     ah, 09h
    int     21h

    add     bx, 2       ; 2 asterisks more each line
    dec     bp          ; 1 space less each line
    jnz     next        ; until no more leading space needed

如果出于某种原因,您不想在底线前面加上一个空格,下一个版本就可以做到这一点:

    mov     bp, 4       ; first line has 4 spaces
    mov     bx, 1       ; first line has 1 asterisk
next: 

    mov     cx, bp      ; current number of spaces
    jcxz    NoSpaces
spaces:
    mov     dl, " "
    mov     ah, 02h
    int     21h
    loop    spaces
NoSpaces:

    ...

    add     bx, 2       ; 2 asterisks more each line
    dec     bp          ; 1 space less each line
    jns     next        ; until no more leading space needed