簡體   English   中英

如何在asm中反轉三角形

[英]How to invert triangle in 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

你的跳躍和循環順序不正確,空格需要自己的計數器,所以我修復了第三個三角形部分:

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   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM