flat assembler
Message board for the users of flat assembler.
Index
> Main > conditional programming IF/THEN/ELSE/ENDIF + loops |
Author |
|
comrade 27 Mar 2013, 04:43
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 |
|||
27 Mar 2013, 04:43 |
|
HaHaAnonymous 27 Mar 2013, 05:17
[ Post removed by author. ]
Last edited by HaHaAnonymous on 28 Feb 2015, 21:15; edited 1 time in total |
|||
27 Mar 2013, 05:17 |
|
wht36 27 Mar 2013, 06:14
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 |
|||
27 Mar 2013, 06:14 |
|
JohnFound 27 Mar 2013, 06:20
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. |
|||
27 Mar 2013, 06:20 |
|
DOS386 27 Mar 2013, 07:51
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 |
|||
27 Mar 2013, 07:51 |
|
AsmGuru62 27 Mar 2013, 13:55
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 |
|||
27 Mar 2013, 13:55 |
|
revolution 27 Mar 2013, 14:00
AsmGuru62 wrote:
Code: add [bNegative], ecx |
|||
27 Mar 2013, 14:00 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.