add cY0,3
cmp cY0,84 ;compare cY0 with 84
jnge ct0 ; jump to ct0 if cY0 >= 84
mov cY0,84 ; else set limit
ct0:
or...
add cY0,3
.IF cY0 >= 84 ; if cY0 >= 84 then...
mov cY0,84 ; set limit
.ENDIF
Readability is one advantage.. am I turning into a bean counter here?
the if then style can be harder to debug. not for the debugger but the person... so its up to you
Sometimes you could save some space replacing:
.IF [myvar ]
with or [myvar], 0
.IF !ZERO?
:)readability. once you start using .if/.then and .while, etc. asm is pretty much on par with C for ease of coding, gleaning the purpose of a block of code, managing. in terms of debugging you could always preserve the original structure by using symbolic debugging.
Readability is one advantage.. am I turning into a bean counter here?
Well, if making your assembly code more readable is turning you into a bean counter, then we should all start counting beans :-).
Seriously, though, it is not possible to make a general recommendation which way to go; you have to consider the use of HLL-like control structures on a case-by-case basis. Most of the time, the readability of the HLL-like control structures is a *very* big win with little loss in efficiency. However, if you get carried away and start writing "C programs with MOV statements" then the code you write will be no better than that produced by a C compiler (indeed, it will probably be worse than what a good C compiler produces), so you don't won't to take this to an extreme if you care about the performance or size of your code.
Cheers,
Randy Hyde
aaaah, .IF .THEN .ELSE... as someone said some time ago...
are we asm coders or what ?
i only use cmp ;)
are we asm coders or what ?
i only use cmp ;)
:) I use IF/THEN/ELSE, it is functionally the same as compare but I don't spend hours and hours figuring out the cmp section of my message loop. Currently Toolbar Paint has an incredibly complicated message loop, it would be insane to have to deal with cmp/jnz, I would spend more time debugging the message loop than the tools.
There are times when you want a pure optimization (where you have no assembler provided constructs and do all the jumping, branching, comparing yourself) such as if you are making a video encoder or comlex realtime radiosity rendering engine :grin: and then other times when you are making a macro optimisation (as I call it) where you know the performance of you code will still be excellent but you're focusing more on making your design work and easily maintainable (such is the case with making some Utitily, like Doney's toolbar builder). In this case, it's okay to let the assembler decide how to manage the cmp/jxx automatically because you know it won't effect the performance enough to make a difference. Instead, you get to spend your time making a product rather than debugging difficult to undertand jmp, cmp, and branching tables.
So you have "pure optimization" where you need absolute control and performance, and you have "macro optimization" where you are more concerned with making the design work and making it readable. In the second case, is 3 clock cylces gonna make a difference if there is 90% idle time anyway waiting for the user response, then it's moot.
The author of the book "Inner Loops" makes a good case that using the .if .else. .while constructs in MASM produces equally efficient code as if it was hand optimized. So it may still not be a major issue.
Thanks,
Shawn
So you have "pure optimization" where you need absolute control and performance, and you have "macro optimization" where you are more concerned with making the design work and making it readable. In the second case, is 3 clock cylces gonna make a difference if there is 90% idle time anyway waiting for the user response, then it's moot.
The author of the book "Inner Loops" makes a good case that using the .if .else. .while constructs in MASM produces equally efficient code as if it was hand optimized. So it may still not be a major issue.
Thanks,
Shawn
thanks for all the insight, I went with a macro. I figure a macro would help me further when it comes time to optimize the section.
How would I do jump-to labels within the macro so it is not errored out as a redefinition of the label?
CMPMAX MACRO A,B
;Usage: CMPMAX A,B - if A >=B then A=B
.IF A >= B
push B
pop A
.ENDIF
ENDM
CMPMIN MACRO A,B
;Usage: CMPMIN A,B - if A <=B then A=B
.IF A <= B
push B
pop A
.endif
endm
How would I do jump-to labels within the macro so it is not errored out as a redefinition of the label?
Define the label as local:
ASDASK MACRO
LOCAL lbl
jmp lbl
;nothing happens here
lbl:
ENDM
ASDASK MACRO
LOCAL lbl
jmp lbl
;nothing happens here
lbl:
ENDM