Hack Forums
[Assembly] how can I compare a dword to an int - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5)
+---- Thread: [Assembly] how can I compare a dword to an int (/showthread.php?tid=2953525)



how can I compare a dword to an int - c0mputer-fr0d - Oct 18, 2012

I want to do this
Code
point      DD ?

LBL:
cmp point,40;
add point,4;



* - K4Fr - Oct 18, 2012

(Oct 18, 2012 05:30 PM)c0mputer-fr0d Wrote:  I want to do this
Code
point  DD ?

LBL:
cmp point,40;
add point,4;
Exactly like you did...Its comparing a dword to an int


* - notsteve - Oct 18, 2012

Why do you have semicolons after each instruction?


* - DaRkReD - Oct 18, 2012

Move them into registers and use atodw

Code
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib

.data
string db '7546754',0
format db 'eax=%u',0
.data?
buffer db 12 dup(?)
.code
start:
invoke atodw,addr string ; eax=7546754
invoke wsprintf,addr buffer,addr format,eax
invoke StdOut,addr buffer
invoke ExitProcess,0
end start



* - D3vildog - Oct 18, 2012

(Oct 18, 2012 09:08 PM)DaRkReD Wrote:  Move them into registers and use atodw

Code
.386
.model flat,stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\masm32.lib

.data
string db '7546754',0
format db 'eax=%u',0
.data?
buffer db 12 dup(?)
.code
start:
invoke atodw,addr string ; eax=7546754
invoke wsprintf,addr buffer,addr format,eax
invoke StdOut,addr buffer
invoke ExitProcess,0
end start
Ooh Darkred, where'd you learn to do MASM? I've only found books on FASM and ROSM and some other ones.


* - Amokov - Oct 19, 2012

What is this DWORD being used for? If it's something numerical, like an address, you can just cmp, you don't need to call any function.


* - Feminist - Oct 19, 2012

(Oct 19, 2012 07:33 AM)Amokov Wrote:  What is this DWORD being used for? If it's something numerical, like an address, you can just cmp, you don't need to call any function.

It depends on the IDE you are using (if any) and compiler. Real assembly cannot do

cmp value, 1

It has to use value as a pointer

i.e.

mov eax, dword ptr [value]

o

cmp dword ptr [value], 40


* - c0mputer-fr0d - Oct 19, 2012

Code
mov ecx dword ptr [point]
    cmp dword ptr [point] ,40

this is what I tried but it doesn't work


* - K4Fr - Oct 19, 2012

(Oct 19, 2012 05:48 PM)c0mputer-fr0d Wrote:  
Code
mov ecx dword ptr [point]
    cmp dword ptr [point] ,40

this is what I tried but it doesn't work
A dword is an int. Secondly cmp only sets flags you still need a branch instruction.
e.g for MASM:
Code
cmp dword ptr [point], 40
40  jne @F  
add ecx, 40  
@@:  
add ecx, 20
@@ is just an unnamed label, @F is forward so it scans forward for @@: @B is back
just use what ever labels are valid in in your assembler


* - .SCHiM. - Oct 20, 2012

cmp dword ptr[ variable ], value is a perfectly valid instruction. That will work.

An int does not differ in size from a dword (on a 32bit machine), the only difference between the two is that an int is signed and a dword is unsigned. On a 64 bit machine an integer type might be 64 bits long, while a dword is always 32 bits long.

To test if both hold the same number (despite their signs) do this:

Code
point dd -40h

mov eax, 40
neg eax     ; negate sign

cmp point, eax
je Equal
    ;; |point| != eax
Equal:
     ;; |point| = eax



* - c0mputer-fr0d - Oct 20, 2012

Thanks everyone and especially schim