我正在尝试为 Game Boy 购物车生成墨盒标题校验和。磁带头校验和定义为类似 C 的伪代码:
unsigned char checksum = 0;
for(unsigned char *p = 0x0134; p < 0x014D; p++)
x += ~*p;
或者:
unsigned char checksum = 0;
for(unsigned char *p = 0x0134; p < 0x014D; p++)
x = x - *p - 1;
该数据从地址开始0x0104
。地址0x0134
对应于title
. 地址0x014D
对应于header_checksum
.
请注意,出于演示目的,我在.ascii
字符串中使用了十六进制转义序列。这实际上不起作用,因为\x
GNU 无法识别as
。
nintendo_logo:
# Nintendo logo. Must be present and unmodified.
.ascii "\xce\xed\x66\x66\xcc\x0d\x00\x0b\x03\x73\x00\x83\x00\x0c\x00\x0d"
.ascii "\x00\x08\x11\x1f\x88\x89\x00\x0e\xdc\xcc\x6e\xe6\xdd\xdd\xd9\x99"
.ascii "\xbb\xbb\x67\x63\x6e\x0e\xec\xcc\xdd\xdc\x99\x9f\xbb\xb9\x33\x3e"
title:
# Title. At most 11 characters and zero-padded if smaller.
# GameBoys use 16 characters.
TITLE_SIZE = 11
.ascii "ABCDEF"
.fill TITLE_SIZE-(.-title)
manufacturer:
.ascii "ABCD"
cgb_f:
# Color Game Boy flag. 0x80 means that the game supports
# Color Game Boy functions, but still works on the original Game Boy.
.byte 0x80
new_licensee:
# Company or publisher ASCII code. 00 is none.
.ascii "00"
sgb_f:
# Super Game Boy flag. 3 means it has support for the SGB, 0 means no.
# I might implement color for the SGB
.byte 0x03
cart_type:
# Memory bank controller used and any additional hardware.
# 0x00 is rom-only.
.byte 0x00
rom_size:
# ROM size in terms of 32KB << B
.byte 0x00
ram_size:
# The amount of externam RAM in the catridge
.byte 0x00
is_japan:
# If the byte is 0x00 it's for Japan, if 0x01 anywhere else
.byte 0x01
old_licensee:
# hex value of company/publisher
# Super Game Boy needs 0x33 to work
.byte 0x33
version:
# version of the game
.byte 0x00
header_checksum:
# TODO: How to calculate this?
sum = add(title, version)
.byte sum
我如何计算这个校验和?如果可能的话,有没有办法使用汇编指令来做到这一点?