Lookup table not jump to address in table

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
MikyBart
Posts: 16
Joined: Tue Sep 03, 2024 9:21 am
Libera.chat IRC: MikyBart

Lookup table not jump to address in table

Post 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?
nullplan
Member
Member
Posts: 1780
Joined: Wed Aug 30, 2017 8:24 am

Re: Lookup table not jump to address in table

Post 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]
Carpe diem!
Post Reply