如何在asm中反转三角形

时间:2016-04-27 02:45:30

标签: 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 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

1 个答案:

答案 0 :(得分:1)

您的跳转和循环顺序不正确,空格需要自己的计数器,所以我修复了第三个三角形部分:

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