跳轉到內容

超級任天堂程式設計/指標

來自華夏公益教科書,開放的世界,開放的書籍

所需工具

[編輯 | 編輯原始碼]

指標是遊戲或 ROM 的重要組成部分。在超級任天堂中,這些指標通常由以特定順序組織的各種地址組成。例如,指標表可能包括遊戲關卡在 ROM 中的位置列表,或指向程式碼特定部分的指標。指標可能與檔名相關;例如,C:\WINDOWS\explorer.exe 指向 WINDOWS 資料夾中的 explorer.exe 檔案。

假設你有一個包含三個關卡的遊戲。你已經將它們插入了你的 ROM 中,但你需要你的遊戲能夠讀取這些關卡。這可以透過在表中使用指向三個關卡的指標來解決,有點像這樣

 .dl Level_1
 .dl Level_2
 .dl Level_3

該表現在將包含 ROM 中關卡 1、關卡 2 和關卡 3 的地址。遊戲隨後可以從該表中讀取,並知道在哪裡查詢這些關卡檔案。

.dl、.dw 和 .db 是 WLA-65816 的三個彙編指令。這意味著它們告訴彙編器執行某些操作,在本例中,它們告訴彙編器建立一個位元組、兩個位元組或三個位元組,這些位元組共同組成一個地址。.db 建立一個單位元組數字,.dw 建立一個雙位元組數字,.dl 建立一個三位元組數字。請注意,儘管在本教程中它們用於指標,但這三個命令也可以用於儲存絕對值。

訪問表

[編輯 | 編輯原始碼]

在 65816 ASM 中,訪問表相對容易。如果你已經熟悉 65c816 中的 A 和 X,那麼下一部分對你來說應該不成問題。

             ; This code will read from a table of 3-byte entries.  Load X with the entry you wish to read, and
             ; the code will read that entry for you.
 LDA Table,X ; This is the important part.  Table here refers to a label, and ",X"  means "Plus the value of X".  So,
             ; A is loaded with the value at (The position of Table) + the value of X.
 JMP ETable  ; Jump past the table, otherwise the CPU will attempt to execute it as code, and easily crash.
 Table:      ; This allows the LDA above to find this table.
 .dw Pointer_1
 .dw Pointer_2
 .dw Pointer_3
 ETable:     ; End of the table.  This label is jumped to by JMP, making sure the 65c816 doesn't try to execute
             ; the data as code.

此程式碼將獲取 X,然後從 (Table + X) 讀取以獲取一個值,然後將其儲存到累加器 (A)。請注意,需要將 X 加倍才能讀取正確的條目。如果 X 為 1,則將從第一個條目的第二個位元組和第二個條目的第一個位元組讀取,這與程式碼的預期操作不符。

華夏公益教科書