flat assembler
Message board for the users of flat assembler.

Index > Main > conditional programming IF/THEN/ELSE/ENDIF + loops

Author
Thread Post new topic Reply to topic
int80



Joined: 25 Mar 2013
Posts: 3
int80
one of the most important instructions/statements in code is conditional statements. coming from a C programming background (20 years), i feel it very difficult to handle it in assembly. let us take an example.

Code:
C code:
column = 0, row = 0;
while(1)
{
        if(row == 25 && col == 80)
                exit();
        else if(col == 80)
        {
                col = 0;
                row++;
        }
        else
        {
                col++;
        }

        gotoxy(col, row); printf("A");
}


in ASM:
        ebp-4 = row
        ebp-8 = col

        mov [ebp-4], 0  
        mov [ebp-8], 0
        
begin_print:
        cmp [ebp-8], 80
        je col_reached_80
        jmp do_nothing

        
col_reached_80:
        cmp [ebp-4], 25
        je everything_done      

        inc [ebp-4]             ; inc row
        mov [ebp-8], 0          ; initialize col to zero

do_nothing:
        inc [ebp-8]
        jmp begin_print


everything_done:
        int 20h
    


now, that the if-elseif-else statements in C leads can be achieved by having many 'labels' in assembly. i am feeling very difficult to label these jumps in assembly. is this how conditional programming done in assembly, or am I doing something fundamentally wrong?

EDIT by DOS386: fixed code layout + enhanced subject
Post 27 Mar 2013, 04:24
View user's profile Send private message Reply with quote
comrade



Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
comrade
Code generation for branches typically evaluates the complement of the condition. For example, given:

Code:
if (x == 5) {
    foo ();
}

bar ();
    


will (typically) generate the following instruction sequence:
Code:
    cmp dword [x], 5
    jne .skip_over
    call foo

.skip_over:
    call bar
    


Techniques such as PGO (profile-guided optimizations) or specifically labeling branches as likely/unlikely (used to be very prevalent in the Linux kernel codebase) can switch this the over way. For example, if x is very unlikely to be 5, then the most likely course of execution is to NOT execute the branch and jump past the if statement. That means whatever is in the branch ("call foo" in the above example) is needlessly taking up space in the instruction cache and might actually slow down the nominal path.

And yes, if the condition is a long expression consisting of multiple evaluations, the machine code can get very long. That is why you should code in C Wink

_________________
comrade (comrade64@live.com; http://comrade.ownz.com/)
Post 27 Mar 2013, 04:43
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:15; edited 1 time in total
Post 27 Mar 2013, 05:17
View user's profile Send private message Reply with quote
wht36



Joined: 18 Sep 2005
Posts: 106
wht36
What is the purpose of the C code? You may be able to simplify your asm code if you code your asm to achieve the same ultimate result rather than code your asm to match C step for step.

E.g. if you are writing to text memory, it is a contiguous array of 80*25 words so to write char + attribute to a text screen at row & column, one could do something like

Code:
mov bx,[bp+4] ;row
imul bx,80*2
add bx,[bp+8] ;column
mov [bx],ax ;write char + attribute    


Another example, to fill a text screen in asm one could code
Code:
mov ax,char + (color shl 8)
mov cx,80*25
rep movsw    


There is also CMOVxx instructions for short code.


Last edited by wht36 on 27 Mar 2013, 07:06; edited 6 times in total
Post 27 Mar 2013, 06:14
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
Using C/C++/HLL code as a prototype for assembly programs is very bad practice. Assembly programming style and assembly programming techniques are very different and this is what make assembly programs superior. Using HLL code for design and prototyping clearly voids this advantages.

The assembly programs must to be prototyped in assembly language only.
Post 27 Mar 2013, 06:20
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
1. I edited your post
2. Use BP in 16-bit DOS code
3. Don't use INT $20 or INT $21 in Win32 code
4. You can use "virtual" to define BP or EBP based variables (but you have to reserve the space too)
5. FASM has macros for MA$M-HLL-style coding (stack-based variables, IF/THEN/ELSE/ENDIF) ... but I don't use them
Post 27 Mar 2013, 07:51
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Using conditional MOV-es or SET-s will reduce amount of labels in ASM code vs. C code.
Example:
Code:
//
// Skip the negative sign in "C"
//
int     bNegative = 0;
char*   pszValue = "-3782.0837";

if (pszValue [0] == '-')
{
        ++pszValue;
        ++bNegative;
}
;
; Same code in ASM (braches eliminated)
;
        mov     esi, pszValue
        xor     ecx, ecx
        cmp     byte [esi], '-'
        sete    cl
        add     esi, ecx
        mov     [bNegative], ecx
    
Post 27 Mar 2013, 13:55
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
AsmGuru62 wrote:
Code:
;...
        ++bNegative;
;...
        mov     [bNegative], ecx    
Perhaps you mean:
Code:
        add     [bNegative], ecx    
Post 27 Mar 2013, 14:00
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-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.