2016-04-27 58 views
2

我不能爲我的數字生活這一點。當我運行這個程序時,前兩個三角形輸出正確,但是我遇到了第三個三角形的問題。 我想要得到的是:如何在asm中反轉三角形

* 
    * * 
* * * 

但我似乎無法得到所需空間的正確金額和我保持在一個無限循環結束了。

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 
+0

步行通過這個和我在一起。當你點擊第二個三角形時,size和bx中的值是多少?如果你輸入3,那麼size應該是3,而ebx也應該是3(對嗎?)。你做的第一件事是'dec bx'(2)。然後你打印bx星星。然後你'dec bx'(1)並循環打印第二行星星。您打印bx星號和'dec bx'(0)並循環打印第三行星。但是因爲bx是零,你打印多少顆星星(記得'loop'遞減cx,*然後*檢查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 
+1

超級清除現在謝謝你! – sippycup