Assembler pyramid program without INT 10H

68 views Asked by At

I have a task to write an Intel 8086 assembly language program that displays a pyramid of numbers without using INT 10h instructions. The result should look like in the screenshot: pyramid

This is my current code and the result

Progr           segment
                assume  cs:Progr, ds:dane, ss:stosik
start:     
                mov     ax, dane
                mov     ds, ax
                mov     ax, stosik
                mov     ss, ax
                mov     ax, offset szczyt
                mov     sp, ax
                ; Clear screen
                mov     ax, 0B800h
                mov     es, ax
                xor     di, di
                mov     cx, 2000
                xor     ax, ax   
                rep     stosw
                mov     bl, 5
                mov     bh, 0
                mov     dh, 1
                mov     dl, 40
                mov     al, 65
                mov     cx, 1
                mov     di, 24
piramida:
                push    cx
wiersz:          mov     ah, bl
                 shl     bx, 1
                 add     bx, dx
                 shl     bx, 1
                 mov     es:[bx], ax
                 dec     cx          
                 jnz     wiersz       
                pop     cx
                inc     dh
                dec     dl
                inc     al
                add     cx,2
                inc     bl
                dec     di
                jnz     piramida
                mov     ah, 4Ch
                mov     al, 0
                int     21h
Progr           ends
dane            segment
dane            ends
stosik          segment
                dw    100h dup(0)
szczyt          Label word
stosik          ends

end start

1

There are 1 answers

0
Sep Roland On
wiersz: mov     ah, bl
        shl     bx, 1
        add     bx, dx
        shl     bx, 1
        mov     es:[bx], ax

This address calculation doesn't make sense! And because the BL register where you have stored the color attribute is part of the wider BX register, you are loosing the correct color information. Why do you even retrieve the color from BL and not just keep it in AH from the beginning? Is it because this whole program is based on an existing version that did use int 10h for character output? I'm sure about this as you have:

mov     bl, 5     ; Color
mov     bh, 0     ; Page
mov     dh, 1     ; Row
mov     dl, 40    ; Column
...
inc     dh        ; Next row
dec     dl        ; 1 column more to the left
inc     al        ; Next ASCII
add     cx,2      ; Wider
inc     bl        ; Next color

and these are exactly what one would expect in the int 10h solution.

The important lesson here is that you must first understand what a piece of code is doing before modifying it.


The regular 80 x 25 text screen uses 160 bytes for each row. You want the top of the pyramid in the middle of the first row, so initialize the address register to 80. Later, in order to descend one row, you will add 160 bytes to this address and subtract another 2 bytes because each level also starts one character more to the left.

There's really not much to it once you get these addresses right:

  mov  dx, 0B800h
  mov  es, dx
  cld
  mov  cx, 1
  mov  bx, 80
  mov  ax, 0541h
Next:
  mov  di, bx
  push cx
  rep stosw
  pop  cx
  add  cx, 2
  add  bx, 160 - 2
  add  ax, 0101h    ; Next character and next color (SWAR-style)
  cmp  al, 'Y'      ; A to X are to be used
  jb   Next

Make sure you understand the code this time, don't just copy/paste and learn nothing...