Qbasicnews.com

Full Version: Memory addressing question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm learning ASM from some book I got out of a library.
I have a few questions about memory addressing:
1. What defines a memory segment? Is it virtual, or is it actually physical chunk of memory?
2. I read that an variable's OFFSET is the distance from the beginning of a segment to the variable. That's fine, except when you're reading a variable from SEGMENT:OFFSET, how does the reader know where to stop reading? I mean, with this bit of ASM:
Code:
.model small    
.stack 100h    
.data
message db "Hello How Are You",'$'    
.code    
main proc      
mov ax,@data  
mov ds,ax      
mov ah,9        
mov dx, offset message  
int 21h
mov ah,4Ch      
int 21h        
main endp    
end main
That code displays a message (assembled in MASM 6.11).
I understand all of it, except:
To display a string with INT 21h, function 9, DSBig GrinX points to the segment:offset of the string. That's fine. But, how does INT 21h know where to stop reading the variable inside the data segment? Wouldn't it just keep reading other data in that segment?

These questions haven't been letting me sleep :evil:
So *someone* answer!!!
Quote:I'm learning ASM from some book I got out of a library.
I have a few questions about memory addressing:
1. What defines a memory segment? Is it virtual, or is it actually physical chunk of memory?
2. I read that an variable's OFFSET is the distance from the beginning of a segment to the variable. That's fine, except when you're reading a variable from SEGMENT:OFFSET, how does the reader know where to stop reading? I mean, with this bit of ASM:
Code:
.model small    
.stack 100h    
.data
message db "Hello How Are You",'$'    
.code    
main proc      
mov ax,@data  
mov ds,ax      
mov ah,9        
mov dx, offset message  
int 21h
mov ah,4Ch      
int 21h        
main endp    
end main
That code displays a message (assembled in MASM 6.11).
I understand all of it, except:
To display a string with INT 21h, function 9, DSBig GrinX points to the segment:offset of the string. That's fine. But, how does INT 21h know where to stop reading the variable inside the data segment? Wouldn't it just keep reading other data in that segment?

These questions haven't been letting me sleep :evil:
So *someone* answer!!!

INT 21h, funciont 9 stops printing when it finds ascii character '0' (that '$' you put on the code). These kind of strings are callez ASCIIZ.

About the SEGMENT:OFFSET it is an old trick when 8086/88 was released. It only had 16 lines to address memory (capable of accessing 64K at a time) but it used "segments" to address up to 1 meg of memory, using 16 bits for an offset and 16 bits for a segment. 1 Meg = 64K*16, so a segment starts every 16 bytes. That means that you can address the same position of memory from different segments, that is, 0000:0010 and 0001:0000 are the same memory pos. The "actual" memory pos is SEGMENT*16+OFFSET, as you may see now.
Ooooh gotcha :rotfl: :rotfl:
One other thing:
Do all INTs and other things that use offsets use a character to tell when to stop reading?
Nope. An INT is an INT, so it takes always 16 bits (2 bytes) and the computer knows exactly where it ends. The same thing happens with all the fixed-length data types (singles, floats, long ints, ints and chars).
Service 9h of int 21h is just a peice of software written by a programmer. It's in no way built into the cpu. This software is loaded at that interrupt by dos at boot time. So you could also make your own interrupt service routines if you wanted to.

It knows that it reached the end of a text when it reaches $ character. This is not a standard, it's just what the programmer that wrote the service routine used. Another programmer might use 0 as string termination (as in C). Or maybe something completly different.

So how does the cpu know how many bytes to move when you do a mov? Simple, you're telling it. By using a reg that is 8/16/32 bits as source/destination.

It's all a matter of an understanding between the source and destination.
Thanks Smile
Actually, I figured those out long ago, but thanks anyway.