Post Pacific  

Go Back   Post Pacific > Lineage 2 > Lineage 2 Server Modifications ONLY! > Extenders

Notices

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 31.03.2008, 22:00   #1
roxaz
Guest
 
Posts: n/a
Default [Tutorial]Memory and data types in asm (for newbies)

Yea, i know what you think, no data types in asm. You are right so shut up and read! Smile


Well there is nothing special about memory. Its an addressable space which contains data. Application allocates or deallocated memory so it can have more memory if needed.


Smallest cell (if we can call it like that) of memmory is called a bit. Bit can be either 0 or 1. Bits are not addressable in memory. However we can get bit values using bit shifting in c++, dunno how to do that in asm, but for you, newbies, its not *must to know* info.


Bigger data type will be byte. It can get any value from 0 to 255. 1 byte = 8 bits. Bytes are addressable data type so you can reach any byte in app if you know its address. Version of byte in asm will be al, ah, bl, bh and so on. How do we know these byte registers? Its simple, wanna make rax to contain byte value? see, rax is 64 bit value of ax register (ax is 16 bits), ax can return its high bytes or low bytes. so al is lower bytes of ax and ah is higher part of the ax bytes. You can put in 2 bytes in ax, store two values in one register. This is cool actualy Very Happy


Next data type is 2 bytes or 16 bits. It is contained in ax, bx, cx etc.. registers.


32 bit values are contained in eax, ebx, ecx etc... registers, see, e letter appears in front of ax, it means extended ax.


and finaly our beloved 64bit registers: rax, rbx. rcx.... here r appears in front of register. i dont remmember wtf it means but its not important any way, what is important is that these registers can carry looooooots of data. maybe you dont think that 8 bytes are a lot, but they are. Incredible numbers are stored in 64bit integers. In example everyone knows function time(). it returns time in seconds counted from 1970 01 01 (if my memory does not fail me) - start of the unix era. At some point 32 bit integer became just too small to contain count of the secconds that passed from that time. Well, 64bit integers saved us!


k, now we know what data types we can have in asm, now we need to know how correctly to put them into memory.

Code:

Code:
mov rax, 123456h  ;we put address of space in memory to rax register
mov [rax], 1  ;here we move 1 to the location of rax
so as you understood [] brackets mean that we will move data not to register, but to location at which register is pointing.

Code:

Code:
mov rax, 123456h
mov rbx, [rax]
smart asses already understood what we did in example above. we moved data to rbx not from rax register, but from location rax is pointing. to. pretty easy.


now be avare of one thing! if you want to read 16 bits of data, read 16 bits! Same is for writing data. this is how we do that:

Code:

Code:
mov rax, 123456h
mov bx, [rax]
we move data from [rax] to 16 bits register. if we moved it to ebx, it would move 32 bits of data. i think you got idea, nothing hard here.


example of writing data:

Code:

Code:
mov [rax], bx
sometimes small misstake may cause your app to crash so be extremely careful!


what if you have address of something in rax and you know stuff you need is somewhere in address relative to rax, about 0x10 bytes further? not a problem! we do it like this:

Code:

Code:
lea rax, [rax+10h]
lea loads address of specified pointer (i assume you know c++, you know wtf pointers are and you saw similarity of [] in asm and * in c++) to the rax register. actyaly this is nice way of doing math. rax gets incremented by 0x10. its same as this:

Code:

Code:
add rax, 10h
it might not look nicely, but if we have address in rbx and we need it incremented by 0x10 and moved to rax then lea gets rly handy:


instead of this:

Code:

Code:
mov rax, rbx
add rax, 10h
we just:

Code:

Code:
lea rax, [rbx+10h]
Equivalents of registers and c++ data types:

ah, al - bool, char

ax - small int

eax - int SIZE in x86 platform

rax - __int64 SIZE_T in x64 platform


So thats it i think. Now you know how to manipulate memory addresses, how to move suoff to and from memory, how to move correct size of needed memory... Good luck in extending l2server ;]


Some useful stuff by others:

Quote:


Originally Posted by Fr3DBr

ax = short =) "okay it can be a wchar_t but why making things complicated ?


bool = logical condition var (true/false) takes 1 bit... but it fits a byte in win32/64 platform.


char = 1 byte (Win32/64)

wchar_t = 2 bytes (Win32/64) Used to Store Extended Characters (AKA Unicode)

short = 2 bytes (Win32/64)

int = 4 bytes (Win32/64)

int64 = 8 bytes (Win32/64)

long = 4 bytes (Win32/64)

long long = 8 bytes (Win32/64)


al = 1 byte -> bool or char

ax = 2 bytes -> short or wchar_t

eax = 4 bytes -> int, long or short (depends of platform)

rax = 8 bytes -> int, long, int64, long long (depends of platform)

xmmX = 16 bytes -> no equivalence, you have to split the value in 2 integers ou doubles.


There are more that are even specific... (X64)

Quote:


Originally Posted by Fyyre

I also recommend http://vx.org.ua/29a/29a-4/29a-4.215 if you're looking to make smaller code...

Восстановлено из публичного снимка Wayback Machine. Тема могла сохраниться частично. В снимке указана только дата, без времени: 1 April 2008. Открыть источник.
 
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 16:54.