flat assembler
Message board for the users of flat assembler.

Index > DOS > segments

Author
Thread Post new topic Reply to topic
Vertex78



Joined: 22 Feb 2004
Posts: 12
Vertex78
Say I write a program like this:

jmp start

variable1 db 25

start:

mov ax,variable1

jmp $

CS point to code segment right?
so does that mean that everything except variable1 is in the code segment?

If so does that mean that variable1 is in the data segment?

If i am on the right track with both of those then does that mean that if DS does not point to your data segment then you program will not be able to access your variables correctly right?

So if you write a program how are you supposed to know what the address to your data segment is?

My last question is, how does this program look in memory? Would it be something like this:

0000 jmp start
0003 variable1 db 25
0004 mov ax,variable1
0005 jmp $

i know my addresses aren't correctly proportioned. But if this is how it looks then everything would be in the code segement correct?

so how would it look in memory if there was a separate data segment for variable1?

And I assume all this applies only to real mode correct?

-Vertex78
Post 22 Jul 2004, 15:07
View user's profile Send private message Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
The code that you attached will compile as binary (*.com) file. Actually COM programs are placed into memory at 0x100 offset, so you have to specify it with ORG directive, and your program should look like this:
Code:
jmp start
variable1 db 25
start:
mov ax,variable1
jmp $    

in COM programs all segment registers (CS, DS, ES), are set to the same value, there's common segment for both code and data. In memory it will look likt this:
Code:
0100 jmp start
0103 variable1 db 25
0104 mov ax,variable1
0105 jmp $     
Post 22 Jul 2004, 15:45
View user's profile Send private message Visit poster's website Reply with quote
Bitdog



Joined: 18 Jan 2004
Posts: 97
Bitdog
JMP $ ;endless loop to it'self = oops
you are jumping to the JMP address ? For what?
Post 05 Sep 2004, 05:48
View user's profile Send private message Reply with quote
crc



Joined: 21 Jun 2003
Posts: 637
Location: Penndel, PA [USA]
crc
Quote:
JMP $ ;endless loop to it'self = oops
you are jumping to the JMP address ? For what?


It's a good way to halt the entire system under DOS or in a single-tasking OS Smile
Post 05 Sep 2004, 10:34
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
vertex: try my tutorial, it is not fully descrtibed but you may learn something
Post 06 Sep 2004, 14:46
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
hartyl



Joined: 31 Aug 2004
Posts: 1
hartyl
crc wrote:
Quote:
JMP $ ;endless loop to it'self = oops
you are jumping to the JMP address ? For what?


It's a good way to halt the entire system under DOS or in a single-tasking OS Smile

when entering ring0 in a OS like windows, you can do a
Code:
cli
jmp $
    

to hang the system Smile
Post 06 Sep 2004, 18:46
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
that is not enough on my windos,
you must do this:

Code:
.mainloop:
cli
jmp .mainloop
    


but if you prefer you can do this:

Code:
mov ss,ax
times 30 push $1234
    


you needn't try hard to make your machine hang or reset, i have discovered many ways mysef.

MATRIX
Post 20 Sep 2004, 09:03
View user's profile Send private message Visit poster's website Reply with quote
izhbq412



Joined: 21 Oct 2004
Posts: 3
izhbq412
Quote:
If i am on the right track with both of those then does that mean that if DS does not point to your data segment then you program will not be able to access your variables correctly right?

I guess so.
Acutally this doesn't work:
Code:
format MZ

entry   .code:start
stack   100h

segment .code use16
start:
        nop
        mov     ax, [b]

        mov     ax, 4C00h
        int     21h

segment .data use16
        b       dw      5     

Code:
AX=0000  BX=FFFF  CX=FE32  DX=0000  SP=0100  BP=0000  SI=0000  DI=0000
DS=0C1C  ES=0C1C  SS=0C2E  CS=0C2C  IP=0001   NV UP EI PL NZ NA PO NC
0C2C:0001 A10000        MOV     AX,[0000]                          DS:0000=20CD    

So after mov ax, [b] is executed the value in AX becomes 20CD, not 5.
As this is not a .com executable there is no org 100h required as far as I know. If I move the data definition in the .code segment and use [cs:b] instead of [b] it works just fine.
I've just started writing in assembler (I've been reading assembler and writing inline asm code in C++ for quite some time, never really programmed complete asm programs though) and this is the first serious problem I can't handle by myself Wink

EDIT: Hehe, I handled it after all Smile Had to load .data to a general register and then mov to ds Wink

_________________
The essence of balance is detachment.
To embrace a cause, to grow fond or spiteful, is to lose one's balance
after which, no action can be trusted.
Our burden is not for the dependent of spirit.
-- Mayar, Third Keeper
Post 21 Oct 2004, 06:24
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
if your data is in different segment, you have to set proper values of the segment registers. This should work:
Code:
format MZ

entry   codeseg:start

segment codeseg
start:
        push    codeseg ; or mov ax, codeseg ; mov ds, ax
        pop     ds
        mov     ax, [b]

        mov     ax, 4C00h
        int     21h

segment dataseg
        b       dw      5
    


btw: IMHO, using "." as prefix for segment names is not a good idea, because "." in FASM defines local labels.

Regards.
Post 21 Oct 2004, 06:52
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
bubach



Joined: 17 Sep 2004
Posts: 341
Location: Trollhättan, Sweden
bubach
That tutorial seems really good! Do you mind if i put it on my website? I'll keep all copyrights intact, of course...

/ Christoffer


Last edited by bubach on 13 Feb 2012, 14:14; edited 1 time in total
Post 21 Oct 2004, 08:09
View user's profile Send private message Reply with quote
izhbq412



Joined: 21 Oct 2004
Posts: 3
izhbq412
Which one is faster:
Code:
push dataseg
pop ds    

or
Code:
mov ax, dataseg
mov ds, ax    

? Smile
Or is it exactly the same?

Quote:
btw: IMHO, using "." as prefix for segment names is not a good idea, because "." in FASM defines local labels.

Shte go imam predvid Wink
(V tozi forum ne mi izliza kirilicata Smile)

_________________
The essence of balance is detachment.
To embrace a cause, to grow fond or spiteful, is to lose one's balance
after which, no action can be trusted.
Our burden is not for the dependent of spirit.
-- Mayar, Third Keeper
Post 21 Oct 2004, 08:15
View user's profile Send private message Reply with quote
Matrix



Joined: 04 Sep 2004
Posts: 1171
Location: Overflow
Matrix
Hello,
since i used to do some size / speed optimizations somewhat i can help:

stack is in memory, if its not cached then its about 290MB/s on my machine
push AX = mov [ss:sp],AX
sub sp,2
pop AX = mov AX,[ss:sp]
add sp,2
there is no such push pop, but you can do it 1 bytes Smile
push AL = mov [ss:sp],AL
dec sp
pop AL = mov AL,[ss:sp]
inc sp

my L2 cache is about 4.5 GB/s, L1 is 9.5GB/s,
and my CPU's registers are very fast, many times faster then memory.

this is small in size, and not usingy registers, but its slow
Code:
push dataseg
pop ds
    

or

however this is larger in code and uses a register
Code:
mov ax, dataseg
mov ds, ax
    


so when i can do it, and there are mass calculations to do, i prefer registers.
Post 21 Oct 2004, 08:47
View user's profile Send private message Visit poster's website Reply with quote
izhbq412



Joined: 21 Oct 2004
Posts: 3
izhbq412
I see. Thank you Smile
Post 21 Oct 2004, 17:04
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.