flat assembler
Message board for the users of flat assembler.

flat assembler > DOS > initial cs register is not 100h?

Author
Thread Post new topic Reply to topic
kty11



Joined: 30 Dec 2012
Posts: 21
Location: south korea
I expect cs register contain 100h
because execution starts at 100h
but the following routine prints 0x0192
this routine is for printing cs value

Code:
org 0x100

;printing for cs
push cs
;left most 4 bits print
l1:
pop ax
push ax
shr ax, 12
cmp al, 9
jg @f
call printdec
jmp l2
@@:
call printhex

;next 4 bits
l2:
pop ax
push ax
shr ax, 8
shl ax, 12
shr ax, 12
cmp al, 9
jg @f
call printdec
jmp l3
@@:
call printhex

;next 4 bits
l3:
pop ax
push ax
shr ax, 4
shl ax, 12
shr ax, 12
cmp al, 9
jg @f
call printdec
jmp l4
@@:
call printhex

;first 4 bits print
l4:
pop ax
push ax
shl ax, 12
shr ax, 12
cmp al, 9
jg @f
call printdec
jmp wexit
@@:
call printhex

;wait until key press
wexit:
mov ah, 00
int 0xf
;exit
mov ah, 4ch
int 21h




printhex:
add al, '0'
add al, 7

mov dl, al
mov ah, 02
int 21h
ret

printdec:
add al, '0'

mov dl, al
mov ah, 02
int 21h
ret

    


could someone tell me why cs contains not 100h?

my assumption:
dos loads .com file to 100h
execute from 100h

is my assumption wrong?

thanks

_________________
since 1990 November 4th
Post 31 Dec 2012, 12:12
View user's profile Send private message Reply with quote
shoorick



Joined: 25 Feb 2005
Posts: 1603
Location: Ukraine
ip (/index/ instruction pointer) has initial value of 100h
cs keeps code segment, where com file has been loaded, its value depends on current system memory situation: so more memory used by drivers and tsrs, so larger value has the new segment.

full physical address can be calculated as cs*16+ip
near jumps/calls change only ip, cs for them stay unchanged


Last edited by shoorick on 03 Jan 2013, 17:14; edited 1 time in total
Post 31 Dec 2012, 13:00
View user's profile Send private message Visit poster's website Reply with quote
kty11



Joined: 30 Dec 2012
Posts: 21
Location: south korea
shoorick wrote:
ip (index pointer) has initial value of 100h
cs keeps code segment, where com file has been loaded, its value depends on current system memory situation: so more memory used by drivers and tsrs, so larger value has the new segment.

full physical address can be calculated as cs*16+ip
near jumps/calls change only ip, cs for them stay unchanged


now I got it
thanks
.com file loaded not on 100h but cs:100h
and ip cares about only inside cs.

Very Happy

_________________
since 1990 November 4th
Post 31 Dec 2012, 13:41
View user's profile Send private message Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 203
Just to clarify:

IP = instruction pointer NOT the index pointer (x86 is using different registers for index pointers)

In real mode the address that CPU executes is controlled by CS:IP pair of registers as pointed out already and CS "cares" only about CS and IP register "cares" about value IP but you as a programmer must take care about both. Also keep in mind that com files has 64kb limit and there is only single segment for stack, data and code. This means that you can use CS to setup properly DS and ES segment registers to point at your code segment which in turn is data segment as well for example.

Happy New Year!
Post 31 Dec 2012, 20:38
View user's profile Send private message Reply with quote
kty11



Joined: 30 Dec 2012
Posts: 21
Location: south korea
ACP wrote:
Happy New Year!


happy new year! Very Happy

_________________
since 1990 November 4th
Post 01 Jan 2013, 01:29
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1388
Location: Toronto, Canada
The beauty of COM file is that all segments are already set, so programmer does not need to do it.
Post 01 Jan 2013, 12:35
View user's profile Send private message Send e-mail Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4160
Location: 2018
not all. some of them (GS for example) aren't assumed to be set to a particular value depending on the dos version.
Post 03 Jan 2013, 14:33
View user's profile Send private message Visit poster's website Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 203
Ok to get things straight:

DOS does not use FS or GS or any other 386 registers (like EAX or TSS). Thare are two exceptions I can think of right now:

1) running DOS under some task switcher in Protected Mode like DesQView or 16/32bit Windows (like 3.0 for example)
2) running your code within DOS Extender - some DOS extenders supported even COM files written directly in assembly language out of the box like Pro32 for example

Basically COM file format (since it is a plain binary just loadded at 100h offset to make room for PSP structure in the same memory segment) has roots in CP/M and that is why it so simple. At the time is has been designed and DOS loader for COM files has been written noboudy had though about 386 features because it wasn't even available back than.

So to not complicate things further here is a setup of segment registers when original, plain MS-DOS loads COM file into memory:

CS,DS,ES,SS = code segment with PSP
SP = FFFEh (the last word in the program segment)

IP is being set to 100h

And this is how all DOS clones and MS-DOS/PC-DOS itself behaves up to 6.x version.

Hope this helps
Post 05 Jan 2013, 21:28
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
ACP,

sp can be different in a memory-scarce scenario. It is loaded (before push 0) with one more than highest address available in ss segment, for regular situation it's (1)0000. There are sources available, you may read them for some information.

fs was used at least in HIMEM.SYS 3.2, for fast m/m transfers in unreal mode (it even retains that mode afterwards).

DESQview was probably the most virtualizing shell ever.

Back to the topic: nobody have to speculate on loader's behavior. Let's write (again) register dumping .COM and try it in every available environment, with some posts of results.
Post 05 Jan 2013, 22:28
View user's profile Send private message Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 203
I'm glad you did not point out that (some) BIOSes also use unreal mode before HIMEM.SYS does during system startup Wink

I actually forgot about HIMEM but since unreal mode requires you to switch to protected mode first it is a very special case.

Don't know which sources are you actually referring too (except HIMEM.SYS sources which indeed has been released) but you can always PM me. I just hope you are joking about checking every possible environment
Post 05 Jan 2013, 22:42
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
ACP,

The sources I've mentioned was 3.30 and 6.0, they're available.

I didn't refer to BIOS because we were talking about DOS, aren't we? And I don't think we have to discuss SMM and other peculiar techniques implemented in BIOS.

Every possible environment? Heh, I'd keep logs with dumps of registers' values on entry to MBR, just for kicks. They even have stack dump for backtracking into those BIOSes, ranging from AWARD thru AMI then MRBIOS and such. Heh, several NICs (3Com, Intel, Compex and now-ubiquitous Realtek) PXEs were the objects for examination (and dissection too).
Post 05 Jan 2013, 23:36
View user's profile Send private message Reply with quote
ACP



Joined: 23 Sep 2006
Posts: 203
Quote:
I didn't refer to BIOS because we were talking about DOS, aren't we? And I don't think we have to discuss SMM and other peculiar techniques implemented in BIOS.


In such case you should also make clear if we are talking about DOS running in plain real mode or in some form of protected mode Wink Please ignore it - I'm just joking Smile
Post 05 Jan 2013, 23:51
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-2018, Tomasz Grysztar.

Powered by rwasa.