Zsolt Z.
Zsolt Z.

Reputation: 599

Is there a way to pass a function as a parameter in assembly?

I'm thinking something like setting the stack pointer to a function, then executing until it returns.

Edit: I use nasm and nlink.

Edit2: I use x86 processor.

Edit3: All right, so I want to write a filter function, that would filter a string, using another function that decides if a char is acceptable or not. I imagine it in the form of:

;params: string in r1, function in r2
Filter:
    ;do stuff
    ret

IsCharGood:
    ;decide if char is good
    ret

main:
    mov     r1, theString
    mov     r2, IsCharGood
    call    Filter

Edit4: Solved, see my answer below.

Upvotes: 0

Views: 109

Answers (2)

Zsolt Z.
Zsolt Z.

Reputation: 599

Ok, so it's relatively simple:

aFunction:
    ;do stuff here
    ret

callFunctionInEax:
    call    eax
    ret

main:
    mov     eax, aFunction
    call    callFunctionInEax
    ret

Upvotes: 1

unwind
unwind

Reputation: 399803

There are no "functions" in assembly, that's a higher-level concept.

You don't say which processor your working on, but if the processor has an instruction to jump to an address in a regular register, that's of course the easiest way. For instance ARM can do this, and so can x86.

And of course how you pass parameters to a sub-routine is up to you as the programmer in assembly. You can pass in registers, or on the stack, that doesn't matter as long as you can generate the call. Hacking the stack pointer itself seems awkward and strange.

Upvotes: 2

Related Questions