TASM:尝试使用指令修改常量时出错

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

我在 TASM 中遇到 "argument needs type override" 错误,因为我试图增加一个常量值。我也不能使用变量,因为这样我就不能简单地将它们相加并得到 "operand types do not much" 错误。有没有办法增加一个常量或增加两个变量?我试过使用寄存器——我的意思是将常量复制到一个寄存器,然后增加寄存器的值,它起作用了,但这并不能解决我的问题。感谢您的任何建议。

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

您的代码需要这些更改:

  1. 将常量转换为变量
  2. 由于变量的原因,您需要拆分添加项。
  3. 另外,由于变量的关系,需要对数据段进行初始化

这是经过 3 处更改的代码(由 "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

现在填充的矩形思路是画水平线多次递增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