Assembly 如何在asm中反转三角形

Assembly 如何在asm中反转三角形,assembly,x86,Assembly,X86,我一辈子也弄不明白这件事。当我运行这个程序时,前两个三角形输出正确,但是第三个三角形有问题。 我想得到的是: * * * * * * 但我似乎无法获得所需的正确数量的空间,我一直在一个无限循环中结束 org 100h .data Input db "Enter size of the triangle between 2 to 9: $" ;String to prompt the user Size dw ? ; variable to hold

我一辈子也弄不明白这件事。当我运行这个程序时,前两个三角形输出正确,但是第三个三角形有问题。 我想得到的是:

    *
  * *
* * *
但我似乎无法获得所需的正确数量的空间,我一直在一个无限循环中结束

org 100h


.data
Input db "Enter size of the triangle between 2 to 9: $" ;String to prompt the user
Size dw ?               ; variable to hold size of triangle
spot db " $" ; a space

.code
Main proc
Start:
Mov ah, 09h  ; function to display string
Mov dx, offset input ;prompts user for input
int 21h ;interrupt processor to call OS

mov ah, 01h ; DOG get character function #
int 21h; takes user input

sub al, '0' ; subtract ascii value of character zero

mov ah, 0   ;blank top half of ax reigster

mov size, ax ; we use ax instead of al because we used dw instead of db
mov cx, ax ; copy size into variable size and cx reigster     

mov bx, 1                    

call newline


lines:                 ; outer loop for number of lines
push cx
mov cx,bx





stars:                 ; inner loop to print stars

mov ah, 02h   
mov dl, '*'
int 21h


loop stars

inc bx

call newline
pop cx     ; get outer loop value back


loop lines
call newline  




; second triangle   

mov cx, size 
dec bx

lines2:  

push cx
mov cx,bx





stars2:
mov ah, 02h
mov dl, '*'
int 21h

loop stars2

 dec bx

 call newline
 pop cx
loop lines2 

;end 



call newline


 ; third triangle   
mov cx, size
inc bx    



lines3:
push cx
mov cx,bx
spaces:
mov ah, 09h 
mov dx, offset spot
int 21h    

stars3:
mov ah, 02h
mov dl, '*'
int 21h     



loop stars3  
loop spaces       
 inc bx      
 call newline  
 pop cx

loop lines3 
;end   


main endp   


proc newline
mov ah, 02h        ; go to a new line after input
mov dl, 13
int 21h
mov dl, 10
int 21h

ret ;returns back


newline endp

end main

<>你的跳跃和循环没有正确的顺序,空白空间需要自己的计数器,所以我固定了第三个三角形部分:

lines3:

mov bp, size   ;<====== BP USED AS BLANK SPACE COUNTER.
sub bp, bx     ;<====== MINUS ASTERISK COUNTER.
jz  no_spaces  ;<====== IF BP IS ZERO, SKIP SPACES.
spaces:
mov ah, 09h 
mov dx, offset spot
int 21h    
dec bp        ;<======= DECREASE COUNTER.
jnz spaces    ;<======= IF COUNTER NOT ZERO, REPEAT.

no_spaces:

push cx
mov cx,bx
stars3:
mov ah, 02h
mov dl, '*'
int 21h      
loop stars3  

call newline
inc bx      
pop cx
loop lines3 
;end   
行3:

mov-bp,大小;和我一起走过这个。当您点击第二个三角形时,
size
和bx中的值是什么?如果输入3,那么大小应该是3,ebx也应该是3(对吗?)。您要做的第一件事是
dec bx
(2)。然后打印bx星星。然后你
dec bx
(1)并循环打印第二行星星。您可以打印bx星和
dec bx
(0),然后循环打印第三行星。但是既然bx是零,那么您要打印多少个星星(记住,
循环
递减cx,然后检查0)?使用调试器遍历此文件可能会使其更清晰。