flat assembler
Message board for the users of flat assembler.

Index > Windows > Newbie question about cmp operator

Author
Thread Post new topic Reply to topic
Xeon



Joined: 14 Nov 2013
Posts: 5
Xeon 11 Dec 2013, 20:25
Hi there! I've got a problem with cmp operation. I have to simply compare two numbers - one of them must be with sign, for example:

mov eax,-7
cmp eax,0
jb _somewhere

And there's no effect (Application doesn't jump to _somewhere label). What should i change to achieve comparison with 0 and -number? or two signed numbers?
Post 11 Dec 2013, 20:25
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 11 Dec 2013, 20:42
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:05; edited 1 time in total
Post 11 Dec 2013, 20:42
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 11 Dec 2013, 21:11
Xeon wrote:
Hi there! I've got a problem with cmp operation. I have to simply compare two numbers - one of them must be with sign, for example:

mov eax,-7
cmp eax,0
jb _somewhere

And there's no effect (Application doesn't jump to _somewhere label). What should i change to achieve comparison with 0 and -number? or two signed numbers?
For signed values you need to use jl and jg (and their variants). jb and ja are for unsigned comparisons.
Post 11 Dec 2013, 21:11
View user's profile Send private message Visit poster's website Reply with quote
Xeon



Joined: 14 Nov 2013
Posts: 5
Xeon 11 Dec 2013, 21:25
Thanks very much! I have one more question about fpu. Trying to do something like this:

Code:
finit
fild [var1]
fdiv [var2]
fstp [var3]

mov eax,[var3]
ccall [printf],strinformat,eax

; var1 dd 40
; var2 dd 5
; var3 dd 0
; strinformat db "%i"

    

And i got strange, huge number, instead 8 as result of division.
Post 11 Dec 2013, 21:25
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20454
Location: In your JS exploiting you and your system
revolution 11 Dec 2013, 21:33
You need to alter your stringformat from %i (integer) to %f for floats. But also be careful about the single/double thing.

BTW: There is a lot of info posted on the Internet about the printf function and all the different ways to format numbers.
Post 11 Dec 2013, 21:33
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 11 Dec 2013, 21:35
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:05; edited 1 time in total
Post 11 Dec 2013, 21:35
View user's profile Send private message Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1905
DOS386 12 Dec 2013, 15:41
> For signed values you need to use jl and jg (and their variants).
> jb and ja are for unsigned comparisons.

Some time ago I had suggested to put this info prominently into "FASM.TXT" file.
Post 12 Dec 2013, 15:41
View user's profile Send private message Reply with quote
tallpeak



Joined: 19 Dec 2013
Posts: 6
tallpeak 19 Dec 2013, 13:53
jb, or jump if below means the same as jc, or jump if carry [is set]. The carry will be set if there was an overflow beyond all bits of the word size. Let's pretend we are in DOS so that the values are smaller:

MOV AX,FFFFh ; max 16-bit number, also the same as -1
ADD AX,1 ; this overflows 16 bits
JC dest ; same as jb and will take the jump.

Also, CMP is the same as SUB, except for not affecting the destination reg/mem. So CMP AX,1 has the same effect on the flags as SUB AX,1. Only 0 is "below" 1 in unsigned arithmetic, so if we:

MOV AX,0
CMP AX,1 ; this will set the carry, because 0 is below 1

I invented a little trick long ago (perhaps 1992?) for range comparisons, which involves unsigned comparisons and the carry flag. Here's some code for a procedure such as make_uppercase(char *str)

(use 32 or 64-bit registers if you prefer, and use MOVZX if you prefer, but a 16-bit example seems just slightly simpler)

LEA SI,[str]
loop:
MOV AL,[SI]
MOV AH,AL
SUB AH,'a'
CMP AH,26 ; set carry if between a and z (0 to 25).
SBB AH,AH ; set to -1 if lowercase
AND AH,-32
ADD AL,AH
MOV [DI],AL
INC SI
INC DI
OR AL,AL
JNZ loop

JC or JB would jump when the carry is set, so the code above could have been written with a jump, but back when processors didn't do branch-prediction, I was slightly proud of eliminating all jumps from this code. Note that I could have used LODSB and STOSB, eliminating several bytes from this code, but for a while (486?) many processors ran those instructions slower than MOV and INC, and now on modern processors it's probably almost even. Simpler instructions that do just one thing are often faster and easier to parallelize among multiple pipelines, so there's generally no advantage to LODS/STOS instructions other than smaller code size. But I do digress...

(Incidentally, I miss DOS DEBUG; maybe it's worth finding DEBUG.COM somehow and making it accessible to DoxBox just for trying tiny assembly blocks of code like this, but of course you may want a 32/64-bit debugger, like perhaps fdbg.x86asm.net (I just tried; looks great, but I miss being able to assemble one or a few lines at a time as I go, as in DOS Debug or even Borland's Turbo Debugger...nowadays code blocks are generally not writable, making that harder to do, unless the debugger can unlock the pages.))


As for the floating-point code, it seems I've avoided FPU code so far. But it looks like you are dividing by a float, which needs to be in float format. Either load integers onto the floating-point stack (which are converted to floats) and then use the stack-based division(fdivp?), or put your number into floating point form before doing the division. See for example http://board.flatassembler.net/topic.php?t=15733
Post 19 Dec 2013, 13:53
View user's profile Send private message 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.