Page 1 of 1

Lookup table not jump to address in table

Posted: Thu Sep 12, 2024 10:46 pm
by MikyBart
Hi all. I have a subroutine in nasm assembly as follows:

Code: Select all


MAX_SUBS equ 3

Main_Subroutine:
    push rbx
    push rbp
    push rsi
    push rdx
    push rdi

    cmp rax, 1
    jb _Exit
    cmp rax, MAX_SUBS
    ja _Exit

    jmp [_Lookup+rax]
    
subroutine1:
	.
	.
	.
    
    jmp _Exit

subroutine2:
	.
	.
	.
    jmp _Exit

subroutine3:
	.
	.
	.

_Exit:
    pop rdi
    pop rdx
    pop rsi
    pop rbp
    pop rbx
	ret
	
SECTION .data
_Lookup dq subroutine1, subroutine2, subroutine3
when i call the main subroutine (Main_Subroutine) with rax containing the subroutine number nothing happens.
Why do you think?

Re: Lookup table not jump to address in table

Posted: Fri Sep 13, 2024 7:58 am
by nullplan
You are indexing the table with rax*1 instead of rax*8. You are also expecting the number to be between 1 and MAX_SUBS, which looks like an off-by-one error to me. If you made it between 0 and MAX_SUBS-1, then the start would just be

Code: Select all

cmp rax, MAX_SUBS
jae _EXIT
jmp [_Lookup+8*rax]