我目前正在为大学实现Snake,为此我们必须使用TASM。
我的主要游戏数据布局如下(使用C语法):
struct GameLine {
uint8_t direction_bits[10]; // 2 bits per entry, the first entry is invalid since valid x positions start at one
uint8_t collision_bits[5]; // 1 bit per entry, the first entry is again invalid but has to stay zero
uint8_t aux; // padding such that GameLine is 16 bytes big, also used to store other information at times.
};
struct GameData {
struct GameLine lines[23];
} PHYSICAL_GAME_DATA;问题是每帧写入方向位会覆盖较大x个位置的读取冲突位(38是最大位置,但它发生得更早)。我之所以说“读取冲突位”,是因为我无法验证physical_game_data的实际位置,因为我不知道如何指示汇编器(tasm)和/或链接器(tlink)和/或调试器(td)显示这一点。
我的代码中的声明是:
physical_game_data DB 368 DUP (0)
; ..., Some `EQU`s for `aux` data I'm using to store other information
game_data EQU (offset physical_game_data) - 16 ; Since 1 <= y <= 23, this allows addressing a line via `game_data + (y << 4)`我在这里最担心的mov(还有几个,但这两个是使bug可见的两个)是这两个:
; This one is for writing a new direction
mov BYTE [ds:game_data + bx], dh
; ...
; This one is for reading a collision bit to check if the player lost
mov dl, [ds:game_data + 10 + si]但是,查看td中的这些代码会产生以下结果:
mov [bx+05AF],dh
; ...
mov dl,[si+05B8]然而,0x05AF和0x05B8之间的差异是9,而不是10。我目前已经通过在源代码中添加11来“修复”这个问题,这意味着错误不会发生,但我更愿意正确地修复这个问题。
我假设这是我对TASM或x86/x86-16汇编的误解,但我不知道这到底是什么误解。
下面是一个完整的文件,展示了这个问题:
.model tiny
.286
.data
datastart:
physical_game_data DB 368 DUP (0)
; ..., Some `EQU`s for `aux` data I'm using to store other information
game_data EQU (offset physical_game_data) - 16 ; Since 1 <= y <= 23, this allows addressing a line via `game_data + (y << 4)`
.code
ORG 100h
start:
mov ax, seg datastart
mov ds, ax
; This one is for writing a new direction
mov BYTE [ds:game_data + bx], dh
; ...
; This one is for reading a collision bit to check if the player lost
mov dl, [ds:game_data + 10 + si]
; Exit
mov ax, 4C00h
int 21h
end start使用tasm MRE.ASM、tlink MRE.OBJ编译,并使用td MRE.EXE使用td打开,反编译的代码为:
mov ax,48AF
mov ds,ax
mov [bx+0103],dh
mov dl,[si+010C]
mov ax,4C00
int 21同样,0x10C - 0x103是9。
如果感兴趣,我将在Dosbox下运行这段代码。
谢谢!
发布于 2020-09-12 00:34:25
您的问题是BYTE关键字。在MASM模式下,BYTE关键字的计算结果是一个字节的大小: 1。在MASM模式下,它被添加到方括号[]中的表达式中,因为方括号主要用作加法运算符。您需要改为编写BYTE PTR [ds:game_data + bx],尽管您的下一条指令说明您也可以省略它。
https://stackoverflow.com/questions/63850557
复制相似问题