flat assembler
Message board for the users of flat assembler.
Index
> Macroinstructions > Determining number of arguments |
Author |
|
vid 26 Aug 2011, 14:31
One rather generic way is to count arguments at assembly time by using forward at preprocess time:
Code: macro func [args] { common argno = 0 if ~ args eq forward argno = argno + 1 common end if ;here test "argno" } after preprocessing this for "func 1, eax, 3" this becomes: Code: argno = 0 if ~ 1, eax, 3 eq ;true argno = argno + 1 argno = argno + 1 argno = argno + 1 end if ;here test argno Depending on what exactly you need this for, there might be a better solution. |
|||
26 Aug 2011, 14:31 |
|
hafai 26 Aug 2011, 15:42
Thank you. The solution actually works well. I tweaked the idea to count using registers so I can push the number of args onto the stack.
Now I'm having a different issue. FASM is calling my invocation of the macro an illegal instruction. All my other macros are working fine but for some reason when I try to invoke this on it fails. It is invoked as "CALL_PROCEDURE proc_name, [args]" My macro pushes args to the stack and then pushes the number of values pushed onto the stack. It then calls proc_name. I've tried taking out the stack code. I've tried taking out the call instruction. I've tried with args, without args, both fail. I've even taken out all the macro's code so it is empty. I've also tried renaming the macro to make sure I wasn't treading on something that was secretly a hidden reserved word. I also know that my use of two macro files isn't causing the issue. Still illegal instruction. I'm at a loss. All my code does at the moment is invoke this macro which then calls a procedure. |
|||
26 Aug 2011, 15:42 |
|
vid 26 Aug 2011, 16:06
You should post your code for us to help. It's rather hard from your description to help you.
|
|||
26 Aug 2011, 16:06 |
|
hafai 26 Aug 2011, 16:19
Okay. Here is the invoke code.
Code: start: CALL_PROCEDURE Clear_Screen halt: JMP halt Code: ;MACRO: CALL_PROCEDURE proc_name, [args] ;DESCRPITION: Used by calling code to call a procedure with the correct calling convention MACRO CALL_PROCEDURE proc_name, [args] { PUSH RAX ;We use this for a moment and need to preserve it PUSH R15 ;We store RSP from before the parameters were pushed onto the stack in R15. As we will need the current contents again we need to save it MOV R15, RSP ;Now store the current RSP in R15 so we can unwind the stack after returning from this call IF ~ args EQ REVERSE PUSH args ;Pushes the args onto the stack in reverse order END IF COMMON MOV RAX, RSP ;Retrieve the current RSP address SUB RAX, R15 ;Determine how many bytes the stack has grown by ;DIV 8 ;Divide by 8 to determine how many arguments were pushed (In 64-bit all pushes (32-bit isn't available (16-bit pushes are but we don't use them here so we can assume (know) that all pushes were 64-bit))) PUSH RAX ;Push the number of arguments onto the stack CALL proc_name ;Call the procedure MOV RSP, R15 ;Unwind the stack (Remember, the called function should have preserved R15 POP R15} ;Now we get the Caller's R15 back so it can preserve it for it's eventual return. If R15 is 0 then there is nothing to return to. Code: START_PROCEDURE Clear_Screen PUSH RAX ;Save RAX MOV RAX, 0xB8000 ;Set to the beginning of video ram START_LOOP Clear_Screen_Loop, 1000 ;Loop 1000 times since we have to copy over 4000 bytes and we do 4 at a ime MOV DWORD [RAX], 0x0F200F20 ;Copy over 4 bytes ADD RAX, 4 ;Increment the address by 4 END_LOOP Clear_Screen_Loop POP RAX ;Restore RAX END_PROCEDURE Clear_Screen Furthermore I know the Clear_Screen function works because I have used it number times before. I am just using it here to see some output to verify that the procedure is called correctly. The args on the stack don't affect it. I'll be using a CMP though to make sure args are being passed properly when this gets assembling correctly. |
|||
26 Aug 2011, 16:19 |
|
hafai 26 Aug 2011, 17:30
This is embarrassing. Earlier nothing was working because I put my includes at the beginning of the main source file. I just moved the include statement for the file with the "illegal instruction" back to the beginning and lo and behold, it's working. Sorry about that.
|
|||
26 Aug 2011, 17:30 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.