flat assembler
Message board for the users of flat assembler.
Index
> Main > Segments |
Author |
|
baldr 27 May 2010, 19:39
Teehee,
I assume that you're talking about real-address mode (or virtual-8086 feature of protected mode). Memory consists of bytes (because it's minimal addressable unit). To address particular byte in real-address/virtual-8086 mode you need combination of two components, segment selector and effective address, which (using simple arithmetics) translates to physical address (or linear in virtual-8086 mode with paging enabled). Same physical/linear address can have different logical representation (aliasing). That's all. 1. You can use segment registers as you wish. Just make sure that your logical address (segment selector in corresponding register and effective address) is right. 2. Yes, your program can trash anything addressable. Follow the rule I've mentioned in (1). 3. Memory is contiguous array of bytes (though some of them can be not present physically), it's real-address mode addressing that causes the mess with segment registers. When you're reading file from disk, you put its contents in particular bytes with corresponding addresses. If you do it so adjacent bytes from file are written to adjacent bytes in memory, they stay that way. 4. Memory layout of file image depends on the way you put it. For example, when you use DOS service 0x4B "EXEC" with al==1 (load but don't execute) on MZ EXE file, DOS loads file according to its MZ header (skips header, modifies file image using relocations, for example) at the address that it chooses itself. On the other hand, DOS service 0x3F "READ" puts file contents into memory (starting with address specified by you) as is. |
|||
27 May 2010, 19:39 |
|
Teehee 04 Jun 2010, 14:46
when a program starts to execute, DS, CS and SS already contains their respective values, or i must do that?
|
|||
04 Jun 2010, 14:46 |
|
edfed 04 Jun 2010, 16:56
when a program starts, depending on the OS, segments are set or not to specified locations.
one thing, memory is not really splitted in segments. memory is just a continuous array of PHISICAL bytes, up to 1 mB in real mode, up to 4gb in pm, up to 64 gb in IA32e, and up to 1tb in long mode. segments are there to index the memory. in real mode, we use 16 bits segments as index *16, because 16 bits addresses are not enough to access the full megabyte. in protected mode, we use segments descriptors to DESCRIBE memory zones, where it is, how many bytes, and some indicators. all modes after pm are variations of pm. |
|||
04 Jun 2010, 16:56 |
|
baldr 04 Jun 2010, 17:48
Teehee,
Program startup has one common property: cs:(e)ip is the entry point. Content of other registers depends on OS and executable file format. For DOS .Com cs==ds==es==ss==PSP segment selector. MZ .Exe header contains relative values for cs and ss in the header. GUI/console PE .Exe executes under flat memory model, thus cs contains selector for flat code segment, ds==es==ss contain selector for flat data segment, and fs contains selector for special segment with TEB structure of primary thread. |
|||
04 Jun 2010, 17:48 |
|
Teehee 07 Jun 2010, 16:20
Thanks guys.
Just a little question now. If i change, for example, CS value, the processor will stop run my code to run where I set in CS? Without jump requiered? ex: Code: org 100h xor eax, eax mov ax,200h ; any address push ax pop cs ; CPU says: I'm not here anymore xor ebx,ebx ; ignored line ; ignored everything else ; . ; . ; . |
|||
07 Jun 2010, 16:20 |
|
edfed 07 Jun 2010, 16:40
you cannot access CS register directlly, only ret, jmp and int can.
|
|||
07 Jun 2010, 16:40 |
|
baldr 07 Jun 2010, 16:43
Teehee,
Read manual, pop cs has no encoding (actually 0x0F is a prefix of many opcodes). mov cs, r/m16 results in #UD. |
|||
07 Jun 2010, 16:43 |
|
revolution 07 Jun 2010, 16:59
Teehee wrote: Just a little question now. If i change, for example, CS value, the processor will stop run my code to run where I set in CS? Without jump requiered? |
|||
07 Jun 2010, 16:59 |
|
Teehee 07 Jun 2010, 17:19
oh.. ok..
but then why fasm compiles pop cs? So I can change any segment except CS? In case of building an OS i will control CS by using GDT/LDT, right? |
|||
07 Jun 2010, 17:19 |
|
revolution 07 Jun 2010, 17:55
Teehee wrote: but then why fasm compiles pop cs? Teehee wrote: So I can change any segment except CS? Teehee wrote: In case of building an OS i will control CS by using GDT/LDT, right? |
|||
07 Jun 2010, 17:55 |
|
bitshifter 08 Jun 2010, 03:02
Teehee wrote:
You are free to do whatever you want with CS, just make sure there is code waiting.. Lets assume we have loaded some code at 0x0050:0x0000 and will go to it... Example using retf Code: push 0x0050 ;segment push 0x0000 ;offset retf Example using far jmp Code: jmp 0x0050:0x0000 Example using far call Code: call 0x0050:0x0000 Once you learn the difference in the 3 you will easily choose what will be the correct one to use in the given circumstance... Note: A call pushes a return IP where the others do not... _________________ Coding a 3D game engine with fasm is like trying to eat an elephant, you just have to keep focused and take it one 'byte' at a time. |
|||
08 Jun 2010, 03:02 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.