flat assembler
Message board for the users of flat assembler.

Index > Main > Problem with IF | if vs .if | Dilemma: to IF or NOT to IF

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
fasmnewbie



Joined: 01 Mar 2011
Posts: 553
fasmnewbie
uart777 wrote:
fasmnewbie:
Quote:
I have to agree with you on some point about using the HL constructs at the beginning of the learning curve
I agree, it's your choice. When I was learning ASM (about 17-19 years ago), I never used .if because I thought it was important to learn "real ASM" first. I was a beginner, not experienced in macros and did not know what code is produced by MASM's .if/.while.

Now, I use my own .if to increase production, especially in my main source files for calling and general program flow. Iczelion was a famous ASM programmer who used .if/.while/etc very clean and professionally in 30+ WinASM examples: http://win32assembly.programminghorizon.com/tutorials.html

I_inc wrote:
Quote:
If you're interested in more opinions, then mine is the opposite one
My opinion is that we should be allowed to write code however we want. It is John with his communist mentality who tries to take away their freedom and impose a style on them that can't be logically defended.

Quote:
I'd suggest C
So, you recommend using a C compiler instead of FASM (as if all C compilers are the same). Why would you assume that any random assembler (for C compiler) is better than FASM? Why should we not use FASM as the assembler/linker/backend? Which assembler is best for this purpose?

Quote:
I don't find any of the uart777's arguments to be valid
Advantages of .if versus cmp-jxx

* .if can be portable, compatible with any CPU: X86, ARM, MIPS, JVM, etc. On ARM - most widely used CPU in the world - cmp v1, 12345678h and cmp v1, [m] are not real ASM instructions, you'd have to create HL macros to support them.
* cmp-jxx requires the programmer to manually create labels whereas .if automatically generates unique labels.
* .if is easy to translate to/from other languages, algorithms and pseudo code.
* .if does not require the programmer to stop and think to invert the condition. Example: "if less" = "if not greater or equal".
* .if condition is written on the same line.
* .if can be edited and/or extended to support calls.
* .if is designed for high-volume production and making real-life applications.
* .if is pure English. "cmp" is not a real word

Here are some arguments that I would love for you to disprove. I challenge anyone to improve my design for a HL compiler: http://board.flatassembler.net/topic.php?t=15588


TQVM for the interesting points on the .if/if and the multi-platform ASM. Quite an eye opener. I_inc and John meant nothing bad though. They were just showing it to me where exactly I did wrong on a very specific problem. You did help too. Thanks.

If you look closely at my posts (even my previous posts), my point of angle is mainly pedagogical. I am not an assembly programmer myself. I am currently working for a faculty and I am here for an independent evaluation purposes. The faculty is restructuring the programming courses and one of the main question we need to answer to the higher-up is why brilliant HLL students perform so badly during the assembly courses. You'll be surprised to see some of our preliminary findings - with unstandardized x86's view of memory being one of them - a pedagogical "bug" inherited much earlier from the HLL (C/C++, FORTRAN, JAVA) courses.

And that ARM platform you mentioned earlier did catch my attention from "faculty" point of view. Thanks.
Post 17 Jul 2013, 09:09
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
fasmnewbie wrote:
…why brilliant HLL students perform so badly during the assembly courses.
Not a bug surprise, to me at least. HLLs introduce many abstract concepts that are not inherent to machine code (and assembly language mainly thereof). Even C (that's what I call high-level assembler Wink) suffers from that. While comma operator is understandable and straightforward (do something and enjoy side-effects), ternary ?: often is a point to stumble upon (I mean efficient LL implementation).

----8<----
uart777,

Macros are powerful in fasm, nonetheless they can't replace human brain (heh, Intel guys, who wrote their C compiler, sometimes fail spectacularly).

Anyway, trying to outperform compiler at optimizing using fasm macros is futile attempt, I think.

Mnemonics ain't words. For a wordy HLL with almost pure English, try COBOL. Wink
Post 17 Jul 2013, 11:35
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
My main issue with .if is the inability to track the code flow.
The code flow is important for developing the fastest running code.
I am talking about the default branch prediction.
Predicted conditional branches are faster.

.if makes developing code faster, but it may not be the fastest running code.
Post 17 Jul 2013, 12:44
View user's profile Send private message Send e-mail Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
AsmGuru62,

Fastest running code (does it exist considering all those differences regarding CPU?) may be developed manually, yet it requires too much efforts (for any non-trivial task). Those macros are kinda shortcuts that are useful to make code more readable, nothing more.
Post 17 Jul 2013, 13:31
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
I agree. Readable code is very important.
I write good code with good label names and comments.
Smile
Post 17 Jul 2013, 15:39
View user's profile Send private message Send e-mail Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
fasmnewbie: Thanks for giving me something to talk about. No matter how much we learn, there's always so much that we don't know and we're all newbies at certain things (example: I'm a beginner at MIPS ASM).
Quote:
John meant nothing bad though.
No personal issues. For years, I've been frustrated by the statements he makes: "Do not use HL features" (while he uses them extensively), "You better write real ASM" (strictly by his definition), "You better not use .if". I don't know what is the translation and meaning in his native language, but in English, it seems demanding like giving an ultimatum: "Do it or else". If he is sincerely trying to help, then I apologize for the misunderstanding.

As for I_inc, never had a problem with him. I have learned quite a bit about FASM's preprocessor from people who come here and post macros/examples. I am thankful for people like this; in a way, they are partially responsible for the programs I create and there is a chain reaction of positive effects when you consider those who may be inspired my creations (?).

Quote:
My main issue with .if is the inability to track the code flow. The code flow is important for developing the fastest running code. I am talking about the default branch prediction. Predicted conditional branches are faster.
What you're talking about has nothing to do with .if or cmp specifically, but rather, the probability of a true condition. You seem to be blaming the tool for ones inability to use it properly. I think there is a right way of using .if/cmp-jxx and goto and a wrong way. I am not trying to convince anyone to use .if, only defending its use for those who are serious about production.

What do you think about C/C++ programmers who say "Don't use goto"? Isn't that stupid? goto is a straight-forward, unconditional jmp (ip+n), but in their minds, goto is like cheating or a HL feature, "not real C/C++". New C/C++ programmers are taught to write code that intentionally avoids the use of goto in situations where it seems perfectly logical (example: to exit a loop) and would create fast executable code. Writing the equivalent with ass-backwards "if+for (x;y;z)" loops that are deeply nested will confuse HL compilers and produce suboptimal code. Don't blame goto for "spaghetti code", blame programmers who write sloppy code!

baldr wrote:
Quote:
Anyway, trying to outperform compiler at optimizing using fasm macros is futile attempt, I think.
So, don't even try? My intention is not to outperform optimized compilers (VC++, Intel C++). Instead, to offer a good standard C-like language that we can optimize. To just get a compiler working with FASM macros is a challenge and we could spend forever optimizing it. Wouldn't it be fun and exciting? Smile

Ultimate language: http://board.flatassembler.net/topic.php?t=15588
Post 18 Jul 2013, 02:45
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
uart777 wrote:
So, don't even try?
I'm not discouraging anyone (indeed, your macros are cool); my grandfather taught me one precious lesson: "use proper tool for each task". fasm preprocessor can be used to transform somewhat higher language into machine instructions, it's just hard to compete with full-blown compiler. OTOH it's pretty hard to use rcl within HL code, for example.
uart777 wrote:
Don't use goto
Dijkstra wrote that he's not against gotos altogether, only those that are counter-intuitive. Jumps are the essential part of any program, HLLs just hide them behind syntactic sugar (I'm still waiting for a language that have equivalent of Forth's BEGIN … WHILE … REPEAT loop).

P.S. Guys, use copy/paste for nicknames, l_inc is neither i_inc nor I_inc. Wink
Post 18 Jul 2013, 07:52
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1409
Location: Toronto, Canada
AsmGuru62
There is a very nice goto equivalent in C/C++ and it is called break.
Also, too deep nesting and 1000 line loops in C/C++ is a sign of illogical code design.
Post 18 Jul 2013, 14:46
View user's profile Send private message Send e-mail Reply with quote
TmX



Joined: 02 Mar 2006
Posts: 821
Location: Jakarta, Indonesia
TmX
goto is used quite extensively in Linux kernel (e.g for error handling).
as long as you're code is readable, I think it's fine.

Linux: using goto in the kernel code
Post 18 Jul 2013, 20:00
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
uart777
Quote:
So, you recommend using a C compiler instead of FASM

I never said "instead". I recommend C as an example of higher level language, that would allow to feel both: convenience of high level constructions and connection to the low level mechanisms.

Moreover I find discussions like "Why FASM? And NOT C/C++" absolutely pointless. Same as "Why screwdriver and not a hammer?", "Why refrigerator and not a microwave?" and "Why a hanky and not pampers?"

baldr
Quote:
Guys, use copy/paste for nicknames, l_inc is neither i_inc nor I_inc

Thank you very much. Smile That's really frustrating, that people don't see the difference when typing the nickname.

_________________
Faith is a superposition of knowledge and fallacy
Post 18 Jul 2013, 22:40
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3500
Location: Bulgaria
JohnFound
About "goto" - read my signature. Smile

I never liked this analogy of programming languages with general ironware tools. I am working with hammers and screwdrivers as well as with programming languages every day.

And can say from personal experience - both have nothing in common. Smile

While with screwdriver you can only turn some screws, in assembly you can program everything.

Can you see the difference?

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 18 Jul 2013, 22:57
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
baldr:
Quote:
my grandfather taught me one precious lesson: "use proper tool for each task"
I understand. For strictly FASM users, FASM is a multi-purpose development tool, only assembler/compiler/language we need to make programs, games, OSs, for any CPU/system and I do not limit its power and capabilities to one simple thing.
Quote:
I'm still waiting for a language that have equivalent of Forth's BEGIN … WHILE … REPEAT loop
Why wait? You know how to write macros Smile

Guru:
Quote:
I write good code with good label names
Are you trying to convince yourself? Smile Let your code speak for itself and let the viewer be the judge.

Anyone who wants to understand HL syntaxes (.if/.while/.for/etc) and how to convert them? And see 1,000s of good names for identifiers? Check out USE\LANGUAGE.INC in: CodeVu+Z77 (old, 2+ months)
Post 19 Jul 2013, 03:32
View user's profile Send private message Reply with quote
uart777



Joined: 17 Jan 2012
Posts: 369
uart777
One more thing: ARM .if/.else may be implemented much differently than X86 because ARM can execute any instruction conditionally. Example:
Code:
; if r0=0, r0=r1+(r2>>16)
; else, r1=r2-(r3>>16)

; ARM: 3 instructions

tst r0, r0
addeq r0, r1, r2, lsr 16
subne r1, r2, r3, lsr 16

; X86 equivelant: 9 instructions, 2 labels
; + possible branch misprediction penalty

test eax, eax
jnz .a
shr edx, 16
add ecx, edx
mov eax, ecx
jmp .b
.a:
shr ebx, 16
sub edx, ebx
mov ecx, edx
.b:    
* My ARM OS (Star-OS): http://board.flatassembler.net/topic.php?t=15534 (old, 3+ weeks, not perfected)
* ARM Information Center: http://infocenter.arm.com/help/index.jsp
Post 19 Jul 2013, 12:30
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 553
fasmnewbie
baldr wrote:
fasmnewbie wrote:
…why brilliant HLL students perform so badly during the assembly courses.
Not a bug surprise, to me at least. HLLs introduce many abstract concepts that are not inherent to machine code (and assembly language mainly thereof). Even C (that's what I call high-level assembler Wink) suffers from that. While comma operator is understandable and straightforward (do something and enjoy side-effects), ternary ?: often is a point to stumble upon (I mean efficient LL implementation).


De-abstracting HLL concepts back to the LL is a PITA. It is even more painful for the teachers/lecturers to do it in much lesser time since ASM is taught only as part of OS, System Organization or something similar. ASM gets all the resources, time and attentions it deserves only in more advanced compiler classes.

For example, students often have difficulties understanding concepts like pointer, memory allocation, linked list, and advanced data structures while on the other hand, they could be easily explained using ASM terminologies and materials (memory, addressing, registers etc). Worse, many of these students taking advance programming courses are not even aware of the specific CPU they are programming.

eg, Ask the student what could be the valid address format returned by this C's statement;

Code:
int myVar; 
printf ("The address is %p\n", myVar);    


The best answer they can come up with is "it returns an address". What kind of address, is it 16-bit, 32-bit or 64-bit address? Then you'll get the idea.
Post 29 Oct 2013, 16:38
View user's profile Send private message Visit poster's website Reply with quote
smiddy



Joined: 31 Oct 2004
Posts: 559
smiddy
Sorry to dredge up an old thread...but I think this is a good starting point for something I'm about to do/run-into. I have to do several logical compares, highly complex, sticking to 32-bit registers up front. I'd like to stay within the x86 construct and was wondering if there are examples to use logical comparisons, of multiple branches, in 32-bit on 64-bit numbers (for now unsigned). Has some one done any examples of this here? Running a search yeilded little help, but this thread is a perfect start, at least since I had no idea how to use either if/.if until reading this thread, I'd always only done multiple CMP in (F)ASM.

So, in pseudo code:
Code:
If EDX:EAX .is_equal. dword [variable_high]:dword[variable_low] .and. ECX:EBX .is_less_than. dword[variable2_high]:dword[variable2_low] 
do something here to dword[variable_high]:dword[variable_low]
...more multiple comparisons, ad nasium    


BTW, I am just wanting the ASM examples, and not a if/.if style of writing.
Post 19 Nov 2014, 12:18
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< 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 YouTube, Twitter.

Website powered by rwasa.