3 Replies - 321 Views - Last Post: 29 December 2018 - 04:10 AM

#1 olimpo31   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-December 18

pyramid in assembly

Posted 27 December 2018 - 10:13 AM

hi I should do this exercise in assembly

                     *
                    ***
                   *****
                  *******
                 *********



but as output I get only the first asterisk.
where is the problem?
this is my code:


.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


thanks

Is This A Good Question/Topic? 0
  • +

Replies To: pyramid in assembly

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2312
  • View blog
  • Posts: 4,419
  • Joined: 30-May 10

Re: pyramid in assembly

Posted 28 December 2018 - 05:53 AM

Better loop names would help see what is going on.
for[123] don't tell us anything useful at all.

.MODEL SMALL
.STACK
.DATA
   nl db 0dh,0ah, '$'
.CODE

    mov ax,@data
    mov ds,ax
    mov cx,5
    mov bx,1

do_row: 
    push cx
    mov dl,20h
    mov ah,2
do_spaces:
    int 21h
    loop do_spaces

    mov cx,bx
    mov dl,'*'
    mov ah,2
do_stars:
    int 21h
    loop do_stars

do_newline:
    lea dx,nl
    mov ah,9
    int 21h

    inc bx
    inc dx
    inc cx

    loop do_row

    mov ah,4ch
    int 21h

END


Some thoughts.
1. You have a push cx without any pop, so you're going to blow up the stack anyway at some point.
2. You increment dx without having initialised it, or ever using it.
3. cx is increasing, not decreasing.
Was This Post Helpful? 1
  • +
  • -

#3 olimpo31   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-December 18

Re: pyramid in assembly

Posted 28 December 2018 - 08:30 AM

View PostSalem_c, on 28 December 2018 - 05:53 AM, said:

Better loop names would help see what is going on.
for[123] don't tell us anything useful at all.

.MODEL SMALL
.STACK
.DATA
   nl db 0dh,0ah, '$'
.CODE

    mov ax,@data
    mov ds,ax
    mov cx,5
    mov bx,1

do_row: 
    push cx
    mov dl,20h
    mov ah,2
do_spaces:
    int 21h
    loop do_spaces

    mov cx,bx
    mov dl,'*'
    mov ah,2
do_stars:
    int 21h
    loop do_stars

do_newline:
    lea dx,nl
    mov ah,9
    int 21h

    inc bx
    inc dx
    inc cx

    loop do_row

    mov ah,4ch
    int 21h

END


Some thoughts.
1. You have a push cx without any pop, so you're going to blow up the stack anyway at some point.
2. You increment dx without having initialised it, or ever using it.
3. cx is increasing, not decreasing.



1 I can not use pop and push well so if you could help me to tell me where to put the pop for cx
2 I've never used right in the program
3 I have never decreased cx
Was This Post Helpful? 0
  • +
  • -

#4 turboscrew   User is offline

  • D.I.C Lover
  • member icon

Reputation: 171
  • View blog
  • Posts: 1,107
  • Joined: 03-April 12

Re: pyramid in assembly

Posted 29 December 2018 - 04:10 AM

Actually, 'loop' decrements [e]cx.

Also, it decrements [e]cx first, and jumps if the result is still non-zero.
(Because at the loop instruction it has already executed the loop body once.)

After for3-loop cx is zero. Then it's incremented (=1) and 'loop' is executed.
'Loop' decrements cx and sees that the result is zero and doesn't jump.

This post has been edited by turboscrew: 29 December 2018 - 04:35 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1