Search code examples
assemblyx86tasm

TASM: error when trying to modify a constant with an instruction


I get "argument needs type override" error in TASM due to trying to increase a constant value. I also can't use variables, because then I can't simply add them to each other and get the "operand types do not much" error. Is there any way to increase a constant or add two variables? I've tried to use registers - I mean copying the constant to a register and then increase the register and it works, but this does not solve my problem. Thanks for any suggestions.

model tiny
.486
.stack 200h

.data
h = 50
w = 100
x = 10
y = 10
k1 = 15
k2 = 0

.code
start:
mov ah, 0      ; graphics mode
mov al, 13h 
int 10h

; main loop
mov cx, 50
pg:
push cx

; top line
mov cx, x+w    ; column
mov dx, y      ; row
mov al, k1     ; white
p1:
mov ah, 0ch    ; put pixel
int 10h
dec cx
cmp cx, x
jae p1

; bottom line
mov cx, x+w
mov dx, y+h 
mov al, k1    
p2: 
mov ah, 0ch 
int 10h
dec cx
cmp cx, x
ja p2

; left line
mov cx, x   
mov dx, y+h
mov al, k1     
p3: 
mov ah, 0ch    
int 10h
dec dx
cmp dx, y
ja p3 

; right line
mov cx, x+w
mov dx, y+h
mov al, k1     
p4:
mov ah, 0ch   
int 10h
dec dx
cmp dx, y
ja p4

; delay
mov cx, 01h
mov dx, 4240h
mov ah, 86h
int 15h

; top line
mov cx, x+w 
mov dx, y    
mov al, k2      
p5:
mov ah, 0ch   
int 10h
dec cx
cmp cx, x
jae p5

; bottom line
mov cx, x+w 
mov dx, y+h
mov al, k2      
p6: 
mov ah, 0ch    
int 10h
dec cx
cmp cx, x
ja p6

; left line
mov cx, x   
mov dx, y+h 
mov al, k2      
p7: 
mov ah, 0ch    
int 10h
dec dx
cmp dx, y
ja p7 

; right line
mov cx, x+w 
mov dx, y+h  
mov al, k2      
p8:
mov ah, 0ch    
int 10h
dec dx
cmp dx, y
ja p8     

pop cx
dec cx
cmp cx, 50
;inc x
;inc y
jna pg

mov ah,00      ; read keyboard
int 16h         

mov ah,00      ; text mode
mov al,03
int 10h

mov ah,4ch     ; exit
int 21h

end start

Solution

  • Your code needs these changes :

    1. Convert your constants into variables
    2. Because of the variables, you will need to split the additions.
    3. Also, because of the variables, it's necessary to initialize the data segment.

    Here is your code with the 3 changes (pointed by "arrows") :

    .model small
    .stack 200h
    
    .data
    h dw 50                   ;<=========================
    w dw 100                  ;<=========================
    x dw 10                   ;<=========================
    y dw 10                   ;<=========================
    k1 db 15                  ;<=========================
    k2 db 0                   ;<=========================
    
    .code
    start:
    mov ax, @data             ;<=========================
    mov ds, ax                ;<=========================
    
    mov ah, 0      ; graphics mode
    mov al, 13h 
    int 10h
    
    ; main loop
    mov cx, 50
    pg:
    push cx
    
    ; top line
    mov cx, x;+w    ; column  ;<=========================
    add cx, w                 ;<=========================
    mov dx, y      ; row
    mov al, k1     ; white
    p1:
    mov ah, 0ch    ; put pixel
    int 10h
    dec cx
    cmp cx, x
    jae p1
    
    ; bottom line
    mov cx, x;+w              ;<=========================
    add cx, w                 ;<=========================
    mov dx, y;+h              ;<=========================
    add dx, h                 ;<=========================
    mov al, k1    
    p2: 
    mov ah, 0ch 
    int 10h
    dec cx
    cmp cx, x
    ja p2
    
    ; left line
    mov cx, x   
    mov dx, y;+h              ;<=========================
    add dx, h                 ;<=========================
    mov al, k1     
    p3: 
    mov ah, 0ch    
    int 10h
    dec dx
    cmp dx, y
    ja p3 
    
    ; right line
    mov cx, x;+w              ;<=========================
    add cx, w                 ;<=========================
    mov dx, y;+h              ;<=========================
    add dx, h                 ;<=========================
    mov al, k1     
    p4:
    mov ah, 0ch   
    int 10h
    dec dx
    cmp dx, y
    ja p4
    
    ; delay
    mov cx, 01h
    mov dx, 4240h
    mov ah, 86h
    int 15h
    
    ; top line
    mov cx, x;+w              ;<=========================
    add cx, w                 ;<=========================
    mov dx, y    
    mov al, k2      
    p5:
    mov ah, 0ch   
    int 10h
    dec cx
    cmp cx, x
    jae p5
    
    ; bottom line
    mov cx, x;+w              ;<========================= 
    add cx, w                 ;<=========================
    mov dx, y;+h              ;<=========================
    add dx, h                 ;<=========================
    mov al, k2      
    p6: 
    mov ah, 0ch    
    int 10h
    dec cx
    cmp cx, x
    ja p6
    
    ; left line
    mov cx, x   
    mov dx, y;+h              ;<=========================
    add dx, h                 ;<=========================
    mov al, k2      
    p7: 
    mov ah, 0ch    
    int 10h
    dec dx
    cmp dx, y
    ja p7 
    
    ; right line
    mov cx, x;+w              ;<=========================
    add cx, w                 ;<=========================
    mov dx, y;+h              ;<=========================
    add dx, h                 ;<=========================
    mov al, k2      
    p8:
    mov ah, 0ch    
    int 10h
    dec dx
    cmp dx, y
    ja p8     
    
    pop cx
    dec cx
    cmp cx, 50
    ;inc x
    ;inc y
    jna pg
    
    mov ah,00      ; read keyboard
    int 16h         
    
    mov ah,00      ; text mode
    mov al,03
    int 10h
    
    mov ah,4ch     ; exit
    int 21h
    
    end start
    

    Now the filled rectangle The idea is to draw the horizontal line many times increasing y :

    .model small
    .stack 200h
    
    .data
    h dw 50
    w dw 100
    x dw 10
    y dw 10
    k1 db 15
    k2 db 0
    
    .code
    start:
    mov ax, @data
    mov ds, ax
    
    mov ah, 0      ; graphics mode
    mov al, 13h 
    int 10h
    
    ; main loop
    mov cx, 50
    pg:
    push cx
    
    ; top line
    mov cx, x;+w    ; column
    add cx, w
    mov dx, y      ; row
    mov al, k1     ; white
    p1:
    mov ah, 0ch    ; put pixel
    int 10h
    dec cx
    cmp cx, x
    jae p1
    
    pop cx
    inc y         ;<============= NEXT HORIZONTAL LINE WILL BE DRAWN IN THE NEXT LINE.
    dec cx
    cmp cx, 50
    ;inc x
    ;inc y
    jna pg
    
    mov ah,00      ; read keyboard
    int 16h         
    
    mov ah,00      ; text mode
    mov al,03
    int 10h
    
    mov ah,4ch     ; exit
    int 21h
    
    end start