flat assembler
Message board for the users of flat assembler.
Index
> Main > multiple conditional statements - dealing with labels |
Author |
|
revolution 26 Jun 2023, 08:31
Code: ccall printf, "number is %d", [number] |
|||
26 Jun 2023, 08:31 |
|
int0x50 26 Jun 2023, 08:59
@revolution .. i don't understand?
What i asked is, when you have multiple if, else, for, conditions, you are supposed to use multiple labels right? how do you deal with that ? like naming lots of labels, keeping it short.. etc. .. sudden introduction of some 'else..if' means, we need to introduce more labels? |
|||
26 Jun 2023, 08:59 |
|
Flier-Mate 26 Jun 2023, 09:14
For sequential number, you can use lookup table instead of evaluating one by one:
For example, Code: quote dd _0 dd _1 dd _2 dd _3 dd _4 dd _5 dd _6 dd _7 dd _8 dd _9 dd _10 dd _11 dd _12 dd _13 dd _14 dd _15 _0 db "a",0 _1 db "b",0 _2 db "c",0 ... ... ... Then in your code, Code: lea ebx, [quote] shl eax, 2 add ebx, eax push 0x40 push title push dword [ebx] push 0 call [MessageBox] eax is the desired number from 0 to 15. (dword is 4 bytes, so shifting left by two) |
|||
26 Jun 2023, 09:14 |
|
revolution 26 Jun 2023, 09:38
There is a set of ".if" macros in the fasm download. Note the leading dot: .if, .else, etc. So you can replicate the C code layout using those. The usage is described in the manual fasm.pdf.
Other label types are the terrible anonymous labels @@, @f, @b. And you can use macros to assign unique names automatically with the "local" keyword. |
|||
26 Jun 2023, 09:38 |
|
bitRAKE 26 Jun 2023, 15:49
[fasmg] There are many ways, but here are a couple extremes:
... first we have a code centric method with arbitrary values known at compile-time: Code: iterate value, condition1,condition2, ... cmp eax, value jz .value ; local label based on value end iterate ... next we have a data centric method, with closed range of values at runtime ... Code: cmp eax, .table.items jnc .default jmp [.table + rax*8] ... .table dq MySpecial0,MySpecial1,MySpecial2,MySpecial3 .table.items := ($ - .table) shr 3 |
|||
26 Jun 2023, 15:49 |
|
int0x50 27 Jun 2023, 05:27
thank you everyone for the response.
is there a way, that fasm (not fasmg) handles labels internally, that i can use it via macro. for example.. earlier i was allocating and handling local variables my self... Code: sub rsp, 20 mov [rbp + 4], 0 ; index mov [rbp + 8], 10 ; index length ... loop_start: cmp [rbp + 4], 0x44 je loop_end .. .. loop_end: then, later i read about local .. it made my life easy ... Code: local .index_i db ? local .index_len dd ? loop_start: mov [.index_i], 0x1 inc [.index_i] so this headache of remembering rbp+4 is i, rbp+8 is len is gone .. like that is there a way to manage labels as part of if, if..else, else, foor loops ... becaue in multiple if, else conditions, we have to manage lots of labels .. not to mention about nested if's... or that's how we have to do, since it's asm ... |
|||
27 Jun 2023, 05:27 |
|
revolution 27 Jun 2023, 11:09
Look into the .if macros. They assign label names automatically. No labels required.
But at the lowest level you need to identify code positions somehow, so if want to avoid macros then you will need to provide the label names manually. |
|||
27 Jun 2023, 11:09 |
|
AsmGuru62 27 Jun 2023, 11:52
Also, the loops themselves need no labels.
FASM provides .while and .repeat macros -- these have no 'break' statement, but you always can JMP out of loop -- then you need a label. I always wanted the 'break', but... this is harsh life. Below is one of REPEAT statements I constructed -- there is one label there to fix an issue, should have made another nested .if: (This was a console application, so speed of the code was not at all important. ) Code: ; ; PRINT 8 QUADRANT VALUES USING FORMAT BELOW (IF QUADRANT WAS SCANNED): ; ; 7 1 6 ; | | |<-- # OF STARS ; | |<---- STARBASE PRESENT ; |<------ # OF KLINGONS: K,C,S ; mov cl, 8 .repeat .if ([rsi + Quadrant.Supernova] = 1) & ([rsi + Quadrant.SupernovaMsg] = 1) mov al, 34 call Console_PutRedAlertText jmp .next .endif .if ([rsi + Quadrant.Scanned] = 1) call SRScan_PrintScannedQuadValue .else ; ; FOR QUADRANTS NOT YET SCANNED -- ONLY STARBASES ARE VISIBLE. ; mov al, rgb_cosmos .if ([rsi + Quadrant.StarbaseMsg] = 1) ; ; IF A STARBASE IS UNDER ATTACK -- LIGHT IT UP WITH YELLOW ALERT. ; mov al, rgb_yellow .endif call Console_SetAttr mov al, 35 add al, [rsi + Quadrant.Starbase] call Console_PutText .endif .next: add rsi, sizeof.Quadrant dec cl .until (ZERO?) |
|||
27 Jun 2023, 11:52 |
|
int0x50 27 Jun 2023, 14:11
thank you @revolution .. I got it now ... do you have any tricks or secrets for labeling ?
thanks @AsmGuru62 ... i understood the power of .if and .endif .... |
|||
27 Jun 2023, 14:11 |
|
macgub 03 Jul 2023, 06:32
revolution wrote:
@revolution Why you hold it as 'terrible'? For me such labels avoid using 'purge' word after each procedure.. Prevent duplicating definitions |
|||
03 Jul 2023, 06:32 |
|
revolution 03 Jul 2023, 07:07
purge is for macros. Nothing to do with labels.
The reason @@ is awful is because it fragile. Using @@ in macros is dangerous. Using @@ outside macros is frustrating when you have to jump around to multiple places. Easier to just add a dot label. Code: cmp ...
js .there
mov ...
add ...
.there: |
|||
03 Jul 2023, 07:07 |
|
macgub 04 Jul 2023, 07:08
From my Fasm experience:
If I use definition such as: Code: .label equ dword[ebp-4] and somwhere in code Code:
.label:
...
jmp .label
I may get compiling error.. Such definitions can cause runtime bugs, too.. If for example I miss declare some labels/definitions.. So to avoid such situations I prefer use @@ labels.. But for sure I use macros very seldom.. Anyway - thanks for info - I will keep in mind this, its valuable for future and now.. |
|||
04 Jul 2023, 07:08 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.