Search code examples
assemblybiosemu8086real-mode

assembly emu8086 diagonal line


I need to draw an diagonal line on my square from the left side to the right i've already the square so i only need the diagonal line i'll leave my square code bellow and this question was not answered yet to emu8086. code:

    org  100h

    jmp code     ; jump into the code section

    ; dimensions of the rectangle:
    ; width: 25 pixels
    ; height:20 pixels

    w equ 25
    h equ 20


    ; set video mode 13h - 320x200

code:
    mov ah, 0
    mov al, 13h
    int 10h


    ; draw upper line:

    mov cx, 125  ; column
    mov dx, 20     ; row
    mov al, 15     ; white
u1:
    mov ah, 0ch    ; put pixel
    int 10h

    dec cx
    cmp cx, 100
    jae u1

    ; draw bottom line:

    mov cx, 100+w  ; column
    mov dx, 20+h   ; row
    mov al, 15     ; white
u2:
    mov ah, 0ch    ; put pixel
    int 10h

    dec cx
    cmp cx, 100
    ja u2

    ; draw left line:

    mov cx, 100    ; column
    mov dx, 20+h   ; row
    mov al, 15     ; white
u3:
    mov ah, 0ch    ; put pixel
    int 10h

    dec dx
    cmp dx, 20
    ja u3


    ; draw right line:

    mov cx, 100+w  ; column
    mov dx, 20+h   ; row
    mov al, 15     ; white
u4:
    mov ah, 0ch    ; put pixel
    int 10h

    dec dx
    cmp dx, 20
    ja u4

    ;wait for keypress
    mov ah,00
    int 16h

    ; return to text mode:
    mov ah,00
    mov al,03 ;text mode 3
    int 10h

    ret

Solution

  • This looks like homework, so no code.

    About the diagonal. In the diagonal, both the row and the column change with every pixel (i. e. every loop iteration). So as you go about the loop increasing CX, you also have to increase DX (or decrease for right-top to left-bottom diagonal). Can you follow up from that?


    In general, your loop variable doesn't have to be the row or the column. You can use a register to count the pixels (i. e. the loop iterations), and calculate the row and the column based on that. If you need more registers, there's SI and DI. If you do that, you can draw more than one pixel per loop iteration. But that's a matter of style and efficiency.