logo elektroda
logo elektroda
X
logo elektroda

Assembler - Understanding .386p Code Segment, Stack Use16, and GDT Setup in Program

smagciu 4114 5
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 4540751
    smagciu
    Level 12  
    Hello. I have a piece of a program here.

    .386p
    
    
    			stack	segment	stack use16
    					dd	100 dup(0)
    			stack	ends
    
    			data segment use16
    
    			gdt		dd	0
    					dd	0
    			code_desc	dd	0000ffffh
    					dd	00cf9a00h
    			data_desc	dd	0000ffffh
    					dd	00cf9200h
    			ret_desc	dd	0000ffffh
    					dd	00cf9a00h
    
    			gdt_label	label	fword
    			gdt_limit	dw	4*8-1
    			gdt_base	dd	?
    
    			protected_code_address	dd	0
    						dw	08h
    			data ends
    
    
    			code segment para public use16 'code'
    			assume cs:code,ds:data,ss:stack
    
    	start:
    			mov	ax,data
    			mov	ds,ax
    
    					; setup gdt
    			mov	eax,0


    and I have a problem what because I don't know what it is about

    stack	segment	stack use16
    					dd	100 dup(0)
    			stack	ends


    and in this

    code segment para public use16 'code'


    Please help.
    I corrected the topic. The code should be placed in the code - krzychoocpp tags
  • ADVERTISEMENT
  • #2 4541108
    Seba85
    Level 17  
    The first fragment declares a stack segment named stack, use16 specifies the stack size, dd 100 dup (0) reserves 100 dd elements with the value 0

    The second fragment declares a segment named code, the pair means that the segment will start with an address divisible by 16, public means that all segments with that name will form one segment, and 'code' specifies the class name for the segment
  • ADVERTISEMENT
  • #3 4541275
    smagciu
    Level 12  
    OKAY. Thanks. Just one more thing. what is this segment class ???
  • ADVERTISEMENT
  • #4 4542593
    Seba85
    Level 17  
    In fact, I have never used segment classes, but from what I remember, this is information for the linker. The linker groups all the segments of the same class so that they are not spread all over the memory, but are in one place.

    As for use16, I said it specifies the stack size, and my point is that it specifies the size of the stack segment, that's exactly 16-bit addressing within the segment.
  • ADVERTISEMENT
  • #5 4543038
    smagciu
    Level 12  
    Even if I understand correctly. What is the difference between 16 and 32 bit code. It's about access to memory, right? 16 bit is 2 bits taken at a time and 32 is 4 at once? I understand well?
  • #6 4543415
    Seba85
    Level 17  
    The effective address is derived from the segment address and the offset. segment address specifies the position of the segment in memory and offset is an offset from the beginning of the segment. With real addressing, segment addresses are stored in segment registers (CS, DS, etc.), and offsets in other segments (CX, DX, ...).

    In the real addressing mode, the base address is 20 bits, but to fit in 16-bit segment registers, the lowest 4 bits are assumed to be zeros, so the base address should be divisible by 16. Hence, effective address = 16 * base_address + offset .

    The size of the segment is limited to the maximum value that the offset can reach, which in turn is limited by the size of the register in which it is stored. In 16-bit mode, a segment size can be up to 2 ^ 16B, or 64kB. In 32-bit mode, a segment size may be 2 ^ 32B, which gives 4GB of address space.

    Since separate instructions are used for 16-bit and 32-bit addressing, you must declare in assembly language that you want to use 16-bit addressing (use16), by default 32-bit addressing (use32) is used. In practice, if you want to run programs under Windows, whether you want to use virtual addressing or not, it doesn't matter which type of addressing you use, but in pure dos, 32-bit addressing is unlikely to go.
ADVERTISEMENT