flat assembler
Message board for the users of flat assembler.

Index > Main > multiple conditional statements - dealing with labels

Author
Thread Post new topic Reply to topic
int0x50



Joined: 19 Jul 2019
Posts: 54
int0x50 26 Jun 2023, 08:27
I am coming from C background .. quite well comfortable and been into C for a very long time ... i want to know from ASM guys, on how do you address these ...

when you have multiple conditional statements, how do you deal with labels?

that means, i have to deal with creating multiple labels ?

or i am missing something obvious ...


Code:
if(number > 100 & number < 1000)
{
        if (number == 100)
                printf("number is 100");
        else if(number == 200)
                printf("number is 200");
        .
        .
        .
        else if(number == 600)
                printf("number is 600");
        else
                printf("...");
                
        for(i = 0; i< number; i++)
        {
                sum = sum + i;
                if(sum == 0)
                {
                        printf("zero");
                }
        }
}
else if(number > 1000 & number < 10000)
{
        if (number == 1000)
                printf("number is 1000");
        else if(number == 2000)
                printf("number is 2000");
        .
        .
        .
        else if(number == 6000)
                printf("number is 6000");
        else
                printf("...");
        for(i = 0; i< number; i++)
        {
                sum = sum + i;
                if(sum == 0)
                {
                        printf("zero");
                }
        }
}    
Post 26 Jun 2023, 08:27
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20334
Location: In your JS exploiting you and your system
revolution 26 Jun 2023, 08:31
Code:
ccall printf, "number is %d", [number]    
Post 26 Jun 2023, 08:31
View user's profile Send private message Visit poster's website Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
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?
Post 26 Jun 2023, 08:59
View user's profile Send private message Reply with quote
Flier-Mate



Joined: 26 May 2023
Posts: 88
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)
Post 26 Jun 2023, 09:14
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20334
Location: In your JS exploiting you and your system
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.
Post 26 Jun 2023, 09:38
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4037
Location: vpcmpistri
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    
... I use a lot of these techniques in my TaskDialog example, if you'd like to see them in situ.
Post 26 Jun 2023, 15:49
View user's profile Send private message Visit poster's website Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
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 ...
Post 27 Jun 2023, 05:27
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20334
Location: In your JS exploiting you and your system
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.
Post 27 Jun 2023, 11:09
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1620
Location: Toronto, Canada
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. Embarassed )
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?)
    
Post 27 Jun 2023, 11:52
View user's profile Send private message Send e-mail Reply with quote
int0x50



Joined: 19 Jul 2019
Posts: 54
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 ....
Post 27 Jun 2023, 14:11
View user's profile Send private message Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 346
Location: Poland
macgub 03 Jul 2023, 06:32
revolution wrote:


Other label types are the terrible anonymous labels @@, @f, @b.


@revolution
Why you hold it as 'terrible'? For me such labels avoid using 'purge' word after each procedure.. Prevent duplicating definitions
Post 03 Jul 2023, 06:32
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20334
Location: In your JS exploiting you and your system
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:    
Then you can use the same code in macros, or out, and you can insert new labels and code without worrying about breaking the chain of @@'s.
Post 03 Jul 2023, 07:07
View user's profile Send private message Visit poster's website Reply with quote
macgub



Joined: 11 Jan 2006
Posts: 346
Location: Poland
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..
Post 04 Jul 2023, 07:08
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.