Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly: Using the Data Segment Register (DS)

Currently I am in the midst of learning x86 assembly for fun, I'm love microcontroller programming, so I'm familiar with assembly.

Currently I've been searching high and low for the answer to this question, but can't seem to find it... the DS register, I know it's supposed to point to the global data in my program, but I don't know how it works exactly. I'm using NASM, and in most simple programs I see the following:

[org 0x7C00]
[bits 16]  

main:
mov ax, 0x0000
mov ds, ax
mov al, [msg]  
mov ah, 0x0E  
mov bx, 0x0007  
int 0x10    
jmp $  

msg db 'X'

times 510-($-$$) db 0  
dw 0xAA55

and that works perfectly (even if I omit the bolded code), but how? Does the CPU automagically load the global variables starting at 0x0000? or is there something intrinsic here that I'm missing?

like image 942
Mykel Stone Avatar asked Feb 04 '11 23:02

Mykel Stone


People also ask

What is DS register in assembly language?

A 16-bit Data Segment register or DS register stores the starting address of the data segment. Stack Segment − It contains data and return addresses of procedures or subroutines. It is implemented as a 'stack' data structure. The Stack Segment register or SS register stores the starting address of the stack.

What is EDI Assembly?

The purpose of this EDI Assembly is to provide a forum for all groups within ALA and ALA-affiliated organizations working on initiatives related to equity, diversity, and inclusion to discuss their activities, identify opportunities for collaboration and coordination, and explore new initiatives related to the ...

WHAT IS EBX register used for?

EBX,BX,BH,BL : Called the Base register It is used as a base pointer for memory access Gets some interrupt return values ECX,CX,CH,CL : Called the Counter register It is used as a loop counter and for shifts Gets some interrupt values EDX,DX,DH,DL : Called the Data register It is used for I/O port access, arithmetic, ...

What is the purpose of segment register?

A segment register changes the memory address accessed by 16 bits at a time, because its value is shifted left by 4 bits (or multiplied by 16) to cover the entire 20-bit address space. The segment register value is added to the addressing register's 16-bit value to produce the actual 20-bit memory address.


1 Answers

When the computer is under real mode (the mode the CPU is at when the BIOS executes the bootloader), the method the CPU uses to calculate the address is very simple: Multiply segment register value by 16 (shift bits 4 positions to left), then add the offset.

For instance in an instruction like "mov ax, [0x1234]" the CPU would use "DS * 0x10 + 0x1234" as the effective address (the first term resolves to zero in your case.) When you have one like "mov ax, [BP+0x32]" then the CPU will use "SS * 0x10 + BP + 0x32". Note that now the CPU used a different segment register (the Stack Segment), and that is because when the BP register is used, the CPU assumes you wan't to access the stack by default (but you can override this by using [DS:BP + 0x32]).

More o less what I've explained and more can be found at http://wiki.osdev.org/Real_Mode and http://www.internals.com/articles/protmode/realmode.htm and lots of more places.

BTW, "msg" should be located more or less at 0x7C11 address.

like image 196
LocoDelAssembly Avatar answered Sep 20 '22 07:09

LocoDelAssembly