I am trying to understand how to use pointer in assembly. By reading some tutorials around internel,I think had undertantood some concepts. But when I’II go to try it,it did work. Below some attempts to translate C to ASM.
C
const char *s = "foo";
unsigned z = *(unsigned*)s;
if(!(z & 0xFF))
do_something();
if(!(z & 0xFFFF))
do_b_something();
(here’s not full code,but it’s a word-check,thefore,there is more two stmts which checks 0xFF0000,0xF000000 respectivily.
ASM:
mov ebp,str
mov eax,ebp
mov eax,[eax]
and eax,0xFF
cmp eax,0
je etc
mov eax,[eax]
and eax,0xFFFF
cmp eax,0
je etc
It returns a seg fault.
And the try:
mov eax,dword ptr [eax]
that’s generated by gcc compiler and you can see it in some other assemblies code,returns
invalid symbol
on FASM assembler. It isn’t really supported by the FASM or am I missing something?
I think this is what you are attempting to do:
Explanation:
First, as JasonD already mentions in his answer, you are loading a pointer to
eax, then doing a logicalandto it, then you are using the result still ineaxto address memory (some memory offset in the range0x0…0xFF).So what goes wrong in your code: you can’t keep in the same register both a pointer to a memory address and a value at the same time. So I chose to load the value from
[eax]toebx, you can also use some other 32-bit general register (ecx,edx,esi,edi) according to your needs.Then, you don’t need to use
cmpto check if a register is empty, because allcmpdoes is that it does the subtraction and sets the flags. But ZF (zero flag) is already set byand, socmpis absolutely unnecessary here. Then, ascmpis not needed here and we do not need the result either, we only want to update the flags, it’s better to usetest.testdoes exactly the same logical AND asanddoes, the only difference being thattestdoes not store the result, it only updates the flags.