As part of our assignment we are supposed to do various functions with matrices. For the menu, we are assigned to use a “case table” (which is implemented as a 2-D array with each row containing only a letter constant and its corresponding function)
I really cannot make sense of the notes, and the book is zero help (it doesn’t mention them at all)
.data
CaseTable BYTE 'A' ; lookup value
DWORD Process_A ; address of procedure
EntrySize = ($ - CaseTable)
BYTE 'B'
DWORD Process_B
BYTE 'C'
DWORD Process_C
BYTE 'D'
DWORD Process_D
NumberOfEntries = ($ - CaseTable) / EntrySize
….
segment .text
...
mov ebx, CaseTable ; point EBX to the table
mov ecx,NumberOfEntries ; loop counter
L1: cmp al,[ebx] ; match found?
jne L2 ; no: continue
call PTR [ebx + 1] ; yes: call the procedure
jmp L3 ; and exit the loop
L2: add ebx,EntrySize ; point to next entry
loop L1 ; repeat until ECX = 0
L3:
Can someone help me make sense of this?
The idea is trivial. If you don’t know enough assembly language, try understanding the following equivalent C code (I took the liberty of defining Process_*() as printing distinct letters and throwing in main()):
Output:
The only problems here can be things like
$andmov ebx, CaseTable.$ evaluates to the assembly position at the beginning of the line containing the expression; so you can code an infinite loop using JMP $.
Hence
EntrySize = ($ - CaseTable)calculates the size of the first entry of the table and likewiseNumberOfEntries = ($ - CaseTable) / EntrySizefirst calculates the entire table size and then divides it by the size of one entry giving you the number of the table entries.Unlike in other assemblers (e.g. MASM and TASM), in NASM
mov ebx, CaseTablemeans loading into ebx the address of the object namedCaseTable. In other assemblers this can mean reading into ebx the first 4 bytes from the object namedCaseTable.Similarly,
DWORD Process_Adefines a DWORD containing the address of the object namedProcess_A.In other assemblers the equivalents may need to be written as
mov ebx, OFFSET CaseTableandDWORD OFFSET Process_A.For the rest, please consult your book, the official NASM documentation and Intel’s / AMD’s x86 CPU manuals. Do your homework, basically. If anything isn’t clear, come and ask specific questions.