Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading address in 16 bit mode

I want to ask how can I simulate C pointers in 16 bit assembly.

int var = 10;
int * ptr = &var;

In assembly it's like

mov dword ptr [ebp-x], 10
lea eax, dword ptr [ebp-x]
mov dword ptr [ebp-x+4], eax

Is there any way how to get physical address of variable on [bp-x] in 16 assembly. For example: I have program which reads sector from floppy, then it jumps to segment:0 and executes it. Program which is being loaded is simple text editor. In editor I need to get physical address of single variable, convert it to segment:offset and use it for loading text file. I have tried to set DS:SI before jump to exitor, but It's not verygood solution. Does anybody know how can solve it? Please help.

like image 202
user35443 Avatar asked Mar 08 '26 04:03

user35443


2 Answers

In the real addressing mode the physical address of a byte of memory is equal to the segment * 16 + offset.

When you refer to memory via [(e)bp+...] or [esp+...], the default segment involved is ss. Otherwise it's ds. An optional segment override prefix will change the default segment register.

So, for example, if your variable is addressed as [bp-8], then its physical address is ss*16+bp-8.

like image 104
Alexey Frunze Avatar answered Mar 09 '26 19:03

Alexey Frunze


So this is your requirement:-

mov word ptr [bp-x], 10
lea ax, word ptr [bp-x] 
mov word ptr [bp-x+4], ax 

You can use some old compiler ,probably that beautiful TCC (Turbo C Compiler, 16 bit). And that will output what you need.

Further even if you will see a 16 bit pointer, its just virtual , and its real address will be translated as per the architecture (like even 32 bit OS run in compatibility mode on an architecture that is 64 bit).

However if you are really very interested doing these kind of stuff, just open cmd -->type debug --> then a -->and you can write a little bit of assembly there.

like image 34
perilbrain Avatar answered Mar 09 '26 17:03

perilbrain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!