Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.msdos.programmer > #4133
| From | Herbert Kleebauer <klee@unibwm.de> |
|---|---|
| Newsgroups | comp.os.msdos.programmer |
| Subject | Re: How to access stack-based data (strings) when SS <> DS ? Newbie language. |
| Date | 2021-12-08 17:39 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <soqn3b$4bs$1@gioia.aioe.org> (permalink) |
| References | (2 earlier) <sol3fh$1g7s$2@gioia.aioe.org> <solj63$3g0$1@gioia.aioe.org> <solr6k$g7g$1@gioia.aioe.org> <som5pl$11ef$1@gioia.aioe.org> <soptt1$77b$1@gioia.aioe.org> |
On 08.12.2021 10:16, R.Wieser wrote:
>> A program is written in order to be executed. To write a program
>> which can be executed on nearly none of the current computers
>> (without first installing additional software like DOSBox) doesn't
>> make much sense. And therefore it doesn't also make much sense for a
>> "starter" to start with 16 bit coding instead of the much easier
>> 32 bit coding.
>
> 1) In relation to "without first installing additional software ". Most
> programming languages need you to install software and configure it. Heaps
> of it. To create 16-bit programs al you need are three executables (an
> editor, the assembler and the linker) and *perhaps* (something like) DOSBox.
It doesn't matter how much software you have to install to create a program.
But once created, it should be possible to execute the program on a
standard Windows installation without first installing additional
software. It is always frustrating if you copy a program on an USB drive
to use it on a different PC and then all what you get is: This program
can't be executed because ......
> 2) Its "much easier 32 bit coding" was, IIRC related to having all segments
> ontop of each other. Funny thing that, as that is exactly what the "tiny"
> memory model does for a 16-bit program. Yes, because it makes things
> simple (my question was just about me thinking "outside the box").
No, that's not the reason. I always used .com programs in DOS so I
never had to mess around with segment registers. But I also used
32 bit registers and addressing modes in 16 bit DOS programs, because
that makes assembly programming so much easier.
> As for using 32-bit (or 64-bit) coding instead of 16-bit ? I don't think
> it matters much - though I think that the best way to learn actual
> programming is /not/ to have too much support from libraries and the like.
To use 32 bit addressing modes has nothing to do with libraries.
> When you have it becomes too easy to create monstrocities instead of the way
> more apropriate (smaller, faster) processor commands. One example I still
> remember is how someone using a higher language isolated a bit in a value :
> (SomeValue & 2^SomeBit) <> 0. Yuck.
This tells the compiler what he has to do, not how it has to be down.
Look at the generated code and you will see, that in most case the
compiler generates better code than an average assembly programmer.
> Bottom line : When learning to really program* I think that using a "dumb"
> target is best. 16-bit DOS programming would be a good choice, both because
> it offers only basic I/O support as well as most people nowerdays have
> computers and thus can do it anywhere (school as well as at home).
Why is 16-bit DOS programming a good choice in the year 2021?
The code below reads from stdin, converts any upper case to lower
case letters and writes it to stdout:
loop: bsr.l getc ; get char from stdin
cmpq.l #-1,r0 ; EOF
bne.b _10 ; branch if not
andq.l #0,r0
bsr.l exit
_10: cmp.b #'A',r0
blo.b _20
cmp.b #'Z',r0
bhi.b _20
add.b #'a'-'A',r0
_20: bsr.l putc ; write char to stdout
br.b loop ; go on
Is this 16 bit DOS or 32 bit Windows programming or is it even
a LINUX program?
> *as opposed to slap-dashing some scripting / high-level language together.
I would even go a step further and directly generate all bytes in
the executable file, this way you can see how the interface to the OS
works.
> In other words : Its not about which language or platform you use, as long
> as it teaches you what makes the bottom layer (the processor and I/O) tick,
> as well as makes you aware that everything comes with a cost (execution time
> and/or resource wise). Those are lessons that should be learned early on.
If it doesn't matter which platform you use, then there is no advantage
in using an obsolete platform instead of a current platform. But it is
a big disadvantage if the generated code can't be executed on a current platform.
Now the answer to the above question, it is neither a DOS, Windows or Linux code,
it is all three at the same time. Just set OS to 0,1 or 2 and the assembler
will generate an executable for the selected OS from the same source code.
Not even a single byte is generated by the assembler or a linker which is
not explicitly specified in the source code.
OS=0 ; 0: DOS 1: WINDOWS 2: LINUX ;;
;===========================================================================
;===========================================================================
IF OS==0 ; DOS
@=$100
ENDIF
;===========================================================================
;===========================================================================
IF OS==1
UseIdatSection=0 ; 0 if no idat section is used
UseUdatSection=0 ; 0 if no udat section is used
dc.w 'ZM',dosfilesize\512, (dosfilesize-1)/512+1
dc.w 0,doshead_end/16,0,$ffff,0,dosstack,0,dosmain
dc.w 0,reloc,0,0,0,0,0,0,0
dc.l 0,0,0,0,0,WinHeader
reloc:
doshead_end:
@=$0
dosmain:move.w s6,-(sp)
move.w (sp)+,s0
move.w #_text,r1
move.b #$09,m0
trap #$21
move.w #$4c01,r0
trap #$21
_text: dc.b 'Nice to meet somebody who is still using DOS,',13,10
dc.b 'but his program requires Win32.',13,10,'$'
even 16
dosstack=@+256
dosfilesize=@+256
ImageBase== $00400000
SectionAlignment== 4096
FileAlignment== 512
WinHeader=@@
@=ImageBase
dc.b 'PE',0,0
dc.w $014c,NumberOfSections
dc.l $36a57950,0,0
dc.w SizeOfOptionalHeader,$010f
@a=@
dc.w $010b
dc.b 5,12
dc.l SizeOfCode,SizeOfInitializedData,SizeOfUninitializedData
dc.l winmain-ImageBase,BaseOfCode,BaseOfData,ImageBase
dc.l SectionAlignment,FileAlignment
dc.w 4,0,0,0,4,0
dc.l 0,SizeOfImage,SizeOfHeaders, 0
dc.w 3 ; 2:GUI 3:character subsystem.
dc.w 0
dc.l $100000,$1000,$100000,$1000,$0,NumberOfRvaAndSize
@b=@
dc.l 0,0,imp_start,imp_size,0,0,0,0,0,0,0,0,0,0,0,0
dc.l 0,0,0,0,0,0,0,0,iat_start,iat_size,0,0,0,0,0,0
NumberOfRvaAndSize = (@-@b)/8
SizeOfOptionalHeader = @-@a
@a=@
dc.b '.text',0,0,0
dc.l VSizeOf_text,VBaseOf_text,FSizeOf_text,FBaseOf_text,0,0
dc.w 0,0
dc.l $e0000020
IF UseIdatSection
dc.b '.idat',0,0,0
dc.l VSizeOf_idat,VBaseOf_idat,FSizeOf_idat,FBaseOf_idat,0,0
dc.w 0,0
dc.l $e0000040
ENDIF
IF UseUdatSection
dc.b '.udat',0,0,0
dc.l VSizeOf_udat,VBaseOf_udat,FSizeOf_udat,FBaseOf_udat,0,0
dc.w 0,0
dc.l $e0000080
ENDIF
NumberOfSections=(@-@a)/40
evencom FileAlignment
SizeOfHeaders==@@
FBaseOf_text==@@
VBaseOf_text==(@-ImageBase+SectionAlignment-1)/SectionAlignment*SectionAlignment
BaseOfCode==VBaseOf_text
@=ImageBase+VBaseOf_text
iat_start=@-ImageBase
KERNEL32_thunk:
ExitProcess:: dc.l KERNEL32_ExitProcess -ImageBase
GetStdHandle:: dc.l KERNEL32_GetStdHandle -ImageBase
ReadFile:: dc.l KERNEL32_ReadFile -ImageBase
WriteFile:: dc.l KERNEL32_WriteFile -ImageBase
dc.l 0
iat_size=@-ImageBase-iat_start
imp_start==@-ImageBase
imp:
dc.l KERNEL32_import -ImageBase
dc.l 0
dc.l 0
dc.l KERNEL32_name -ImageBase
dc.l KERNEL32_thunk -ImageBase
dc.l 0,0,0,0,0
imp_size==@-imp
KERNEL32_name:
dc.b 'KERNEL32.dll',0
even
KERNEL32_import:
dc.l KERNEL32_ExitProcess -ImageBase
dc.l KERNEL32_GetStdHandle -ImageBase
dc.l KERNEL32_ReadFile -ImageBase
dc.l KERNEL32_WriteFile -ImageBase
dc.l 0
even
KERNEL32_ExitProcess:
dc.w 0
dc.b 'ExitProcess',0
even
KERNEL32_GetStdHandle:
dc.w 0
dc.b 'GetStdHandle',0
even
KERNEL32_ReadFile:
dc.w 0
dc.b 'ReadFile',0
even
KERNEL32_WriteFile:
dc.w 0
dc.b 'WriteFile',0
even
seg32
winmain::
ENDIF
;===========================================================================
;===========================================================================
IF OS==2 ; Linux
seg32
@=$08048000
code_offset=@@
code_addr:
;--------------------------- ELF header -----------------------------------
dc.l $464c457f,$00010101,0,0,$00030002,1,main,$34,0,0,$00200034,2,0
dc.l 1,code_offset,code_addr,code_addr,code_filez,code_memsz,5,4096
dc.l 1,data_offset,data_addr,data_addr,data_filez,data_memsz,6,4096
;--------------------------- code ------------------------------------------
main:
ENDIF
;===========================================================================
;===========================================================================
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
loop: bsr.l getc ; get char from stdin
cmpq.l #-1,r0 ; EOF
bne.b _10 ; branch if not
andq.l #0,r0
bsr.l exit
_10: cmp.b #'A',r0
blo.b _20
cmp.b #'Z',r0
bhi.b _20
add.b #'a'-'A',r0
_20: bsr.l putc ; write char to stdout
br.b loop ; go on
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; OS specific functions: getc, putc, exit ;;
;; 0: DOS 1: WINDOwS 2: LINUX ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;===========================================================================
;===========================================================================
IF OS==0 ; DOS
getc: eor.l r0,r0
movem.l r0-r7,-(sp)
move.w #$3f00,r0
lea.w 28.b(r7),r1
move.w #1,r2
eor.w r3,r3
trap #$21
bcs.b _20
cmp.w r0,r2
movem.l (sp)+,r0-r7
beq.b _10
move.l #-1,r0
_10: rts.l
_20: move.b #-1,r0
br.b exit
putc: movem.l r0-r7,-(sp)
move.w #$4000,r0
lea.w 28.b(r7),r1
move.w #1,r2
move.w r2,r3
trap #$21
bcs.b _20
cmp.w r0,r2
bne.b _20
movem.l (sp)+,r0-r7
rts.l
_20: move.b #-1,r0
br.b exit
exit: move.b #$4c,m0
trap #$21
ENDIF
;===========================================================================
;===========================================================================
IF OS==1 ; WINDOWS
getc: eor.l r0,r0
movem.l r0-r7,-(sp)
lea.l 28.b(r7),r1
move.l r0,-(sp)
move.l r7,r2
add.l _handle,r0
bne.b _10
moveq.l #-10,-(sp)
jsr.l (GetStdHandle)
move.l r0,_handle
_10: moveq.l #0,-(sp)
move.l r2,-(sp)
moveq.l #1,-(sp)
move.l r1,-(sp)
move.l r0,-(sp)
jsr.l (ReadFile)
move.l (sp)+,r1
or.l r0,r0
bne.b _20
orq.l #-1,r0
br.b exit
_20: cmp.l #1,r1
movem.l (sp)+,r0-r7
beq.b _30
move.l #-1,r0
_30: rts.l
even4
_handle:dc.l 0
putc: movem.l r0-r7,-(sp)
lea.l 28.b(r7),r1
move.l r0,-(sp)
move.l r7,r2
eor.l r0,r0
add.l _handle,r0
bne.b _10
moveq.l #-11,-(sp)
jsr.l (GetStdHandle)
move.l r0,_handle
_10: moveq.l #0,-(sp)
move.l r2,-(sp)
moveq.l #1,-(sp)
move.l r1,-(sp)
move.l r0,-(sp)
jsr.l (WriteFile)
move.l (sp)+,r1
or.l r0,r0
bne.b _20
_30: orq.l #-1,r0
br.b exit
_20: cmp.l #1,r1
bne.b _30
movem.l (sp)+,r0-r7
rts.l
even4
_handle:dc.l 0
exit: move.l r0,-(sp)
jsr.l (ExitProcess) ; exit program
ENDIF
;===========================================================================
;===========================================================================
IF OS==2 ; LINUX
getc: eor.l r0,r0
movem.l r0-r7,-(sp)
move.l #0,r3 ; stdin
lea.l 28.b(r7),r2
move.l #1,r1 ; 1 byte
move.l #3,r0 ; read
trap #$80
tst.l r0,r0
bmi.b _10
movem.l (sp)+,r0-r7
bne.b _20
orq.l #-1,r0
_20: rts.l
_10: orq.l #-1,r0 ; return code
br.b exit
putc: movem.l r0-r7,-(sp)
move.l #1,r3 ; stdout
lea.l 28.b(r7),r2
move.l #1,r1 ; 1 byte
move.l #4,r0 ; write
trap #$80
cmpq.l #1,r0
bne.b _10
movem.l (sp)+,r0-r7
rts.l
_10: orq.l #-1,r0 ; return code
br.b exit
exit: move.l r0,r3 ; return code
move.l #1,r0 ; exit
trap #$80
ENDIF
;===========================================================================
;===========================================================================
;===========================================================================
;===========================================================================
IF OS==1 ; WINDOWS
VSizeOf_text==@-Imagebase-VBaseOf_text
@a=@
evencom FileAlignment
@=@a
FSizeOf_text==@@-FBaseOf_text
SizeOfCode==FSizeOf_text
FBaseOf_idat==@@
VBaseOf_idat==(@-ImageBase+SectionAlignment-1)/SectionAlignment*SectionAlignment
BaseOfData==VBaseOf_idat
@=ImageBase+VBaseOf_idat
ENDIF
;===========================================================================
IF OS==2 ; Linux
code_filez=@@-code_offset
code_memsz= @-code_addr
even 4
@=(@+4095)/4096*4096+(@@\4096)
data_offset=@@
data_addr:
ENDIF
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;--------------------------- initialized data ------------------------------
;some_initialized_data: dc.l 3
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;===========================================================================
IF OS==1 ; WINDOWS
VSizeOf_idat==@-Imagebase-VBaseOf_idat
@a=@
evencom FileAlignment
@=@a
FSizeOf_idat==@@-FBaseOf_idat
SizeOfInitializedData==FSizeOf_idat
FBaseOf_udat==@@
VBaseOf_udat==(@-ImageBase+SectionAlignment-1)/SectionAlignment*SectionAlignment
@=ImageBase+VBaseOf_udat
ENDIF
;===========================================================================
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;--------------------------- uninitialized data ----------------------------
;some_uninitialized_data: blk.b 20
;---------------------------------------------------------------------------
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
;===========================================================================
IF OS==1 ; WINDOWS
VSizeOf_udat==@-Imagebase-VBaseOf_udat
@a=@
evencom FileAlignment
@=@a
FSizeOf_udat==@@-FBaseOf_udat
SizeOfUninitializedData==VSizeOf_udat
SizeOfImage==(@-ImageBase+SectionAlignment-1)/SectionAlignment*SectionAlignment
ENDIF
;===========================================================================
IF OS==2 ; Linux
data_filez=@@-data_offset
data_memsz= @-data_addr
ENDIF
;===========================================================================
Back to comp.os.msdos.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-04 08:56 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-12-04 12:47 +0000
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-04 15:57 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-12-04 16:13 +0000
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-04 18:41 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-12-04 20:28 +0000
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-05 08:56 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-12-05 10:32 +0000
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-05 13:11 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-12-05 12:21 +0000
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-05 14:59 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-04 19:00 +0100
Re: How to access stack-based data (strings) when SS <> DS ? JJ <jj4public@gmail.com> - 2021-12-05 14:27 +0700
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-05 12:43 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "Alexei A. Frounze" <alexfrunews@gmail.com> - 2021-12-05 22:25 -0800
Re: How to access stack-based data (strings) when SS <> DS ? Mateusz Viste <mateusz@xyz.invalid> - 2021-12-06 09:30 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-06 14:33 +0100
Re: How to access stack-based data (strings) when SS <> DS ? Herbert Kleebauer <klee@unibwm.de> - 2021-12-06 19:01 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-06 21:16 +0100
Re: How to access stack-based data (strings) when SS <> DS ? Herbert Kleebauer <klee@unibwm.de> - 2021-12-07 00:19 +0100
Re: How to access stack-based data (strings) when SS <> DS ? Mateusz Viste <mateusz@xyz.invalid> - 2021-12-07 09:31 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-07 09:37 +0100
Re: How to access stack-based data (strings) when SS <> DS ? Newbie language. "R.Wieser" <address@not.available> - 2021-12-08 10:16 +0100
Re: How to access stack-based data (strings) when SS <> DS ? Newbie language. Herbert Kleebauer <klee@unibwm.de> - 2021-12-08 17:39 +0100
Re: How to access stack-based data (strings) when SS <> DS ? Newbie language. "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-12-08 17:37 +0000
Re: How to access stack-based data (strings) when SS <> DS ? Newbie language. Herbert Kleebauer <klee@unibwm.de> - 2021-12-08 22:31 +0100
Re: How to access stack-based data (strings) when SS <> DS ? Newbie language. "Kerr-Mudd, John" <admin@127.0.0.1> - 2021-12-09 21:43 +0000
Re: How to access stack-based data (strings) when SS <> DS ? Newbie language. "R.Wieser" <address@not.available> - 2021-12-08 22:19 +0100
Re: How to access stack-based data (strings) when SS <> DS ? "Alexei A. Frounze" <alexfrunews@gmail.com> - 2021-12-06 22:40 -0800
Re: How to access stack-based data (strings) when SS <> DS ? "R.Wieser" <address@not.available> - 2021-12-07 10:17 +0100
csiph-web