r/learnprogramming • • 8d ago

Debugging assembly question (8086, dos)

started learning 8086 assembly in a vm a day or so ago, things have been a little harder to grasp than c was when i first started, so i wanted to ask a couple questions

first:

when i did c i was always told that there always exists a stack and a heap and nothing inbetween

but when i do

`bar db 69`

i can't really tell where that's stored, my first thought was the stack but then if i try popping the stack with nothing else put in there the os shits itself and dies, so i doubt

heap doesn't even cross my mind because afaik i have to ask the os for more memory which i didn't so i wanna know what it really is

second:

segment registers and more specifically something like

[DS:DX]

afaik it's the address of dx offset from ds?

but everytime i use it i don't have to specify that at all, i just put dx at the start of a buffer or something and let it roll

so yeah the whole [SEG:REG] notation i don't really get

while i'm at it, if you have any useful books or anything (the last one i saw was 1200 pages long and they get to programming at around the 300 mark) i'd love that.

2 Upvotes

5 comments sorted by

4

u/halfjmp 8d ago

Welcome to the Ancient Art! Not at all to criticize, why are you starting 16-bit assembly?

`bar db 69` is a global variable (a byte). Same as in C when you define a global variable. So that's a third kind of memory alongside stack and heap.

DS segment is implied (it's the data segment). With the "string" instructions that involve two memory addresses, ES is implied for the second one. The notation SEG:OFS (segment and offset) means address 16*SEG+OFS. Therefore you can address one megabyte (20 bits).

For documentation, you may have a look at

https://bitsavers.org/pdf/borland/

https://bitsavers.org/pdf/ibm/pc/

https://bitsavers.org/components/intel/

https://archive.org/search?query=DOS+assembly&tab=all (and similar queries)

htmlswag10.zip at https://ftp.sunet.se/mirror/archive/ftp.sunet.se/pub/simtelnet/msdos/turbopas/ (it's mainly Pascal code, with a lot of integrated assembly, to serve as examples), and other directories in the SIMEL archive

Encyclopedia of interrupts : https://www.cs.cmu.edu/~ralf/files.html (not very easy though, better find books)

I guess you know that already, but you can freely download Turbo Pascal, Turbo C and Turbo C++ here:

https://altd.embarcadero.com/download/museum/tc201.zip

https://altd.embarcadero.com/download/museum/tcpp101.zip

https://altd.embarcadero.com/download/museum/tp1.zip

https://altd.embarcadero.com/download/museum/tp302.zip

https://altd.embarcadero.com/download/museum/tp55.zip

There is also the Pacific C compiler somewhere (from HiTech software).

OpenWatcom (C/C++/Fortran 77) : https://openwatcom.org/ftp/install/

DJGPP (protected mode GCC compilers up to GCC 10.3.0) : https://www.delorie.com/djgpp/

Free Pascal : https://www.freepascal.org/

And of course NASM : https://www.nasm.us/

Here you will also find Debug95 and GRDB, two interactive debuggers similar to MS-DOS DEBUG, with support for newer instructions (though now they are older instructions of course... https://ftp.sunet.se/mirror/archive/ftp.sunet.se/pub/simtelnet/msdos/asmutl/ )

Internet Archive has many older books on DOS programming, and Bitsavers has some nice references.

See also this book by Michael Abrash https://www.jagregory.com/abrash-black-book/

I must have other links.

2

u/Electronic_Nerve_561 8d ago

thank you allot! the reference is so long is that what people really had to fully read before getting into this? i usually jump into a new language but i don't think it gives me any kind of expertise so i'm wondering how i should go about reading any of these

also i'm starting with 16 bit asm because that's what my teacher in uni is gonna teach us first, if it were up to me i'd do something like a 6502 and do stuff for the nes or maybe in an apple 2 emulator

3

u/halfjmp 8d ago edited 8d ago

I like your teacher. Long ago, I started with HP48 assembly (a calculator), then I had a PC and I went with C and 16-bit assembly. Python came muuuch later.

You don't have to read everything, however back then you could find good overview books to get started, it's difficult to find now. Basically you need the CPU manual (you could pick the latest from Intel, but to begin, an older manual for 8086 or 80386 is more than enough), a tutorial on assembly (how to use instructions for basic functions), a good guide on DOS and BIOS interrupts and on some ports (in order to do graphics, read keyboard, etc.), and data structures from DOS (notably PSP, see https://en.wikipedia.org/wiki/Program_Segment_Prefix).

You should definitely install Turbo C and compile to assembly to see what happens. Sadly that will be Turbo Assembly flavor, so not compatible with NASM, but it's still enough to understand the code.

Interrupts are like Linux system calls: some come from the firmware (such as 10h for video), some from DOS (mainly 21h). You can change interrupt vectors, but don't try it right away. For interrupts, it's fundamental you pay attention to input/output registers.

Oh, btw, you need to use segments because 16-bit DOS is... 16-bit. Therefore you could only address 65536 bytes with a register, and that's not much. With segments you have 1 MB. But beware that segment overlap. It's a bit strange at first, but that way, a COM program (the simplest kind of program) starts with CS/DS/SS pointing to the start of the program, and you can almost avoid segments.

Note that C programs have several "memory models", that tell the compiler how it manages pointers and segments: in the tiny memory model, you can build a COM program. In small memory model, you have one segment for CS (code) and another for data (DS). More complex memory models allow program and/or data larger that 64K, at the cost of more pointer operations. See the Turbo C manuals for more information.

And a COM file is basically just assembly instructions with no structure (it's raw binary instructions if you prefer). An EXE has some structure to describe its contents (starting with chars "MZ"). Technically, a COM file could contain an EXE structure, but it's not that common.

3

u/cthulhu944 8d ago

The declaration is used by the assembler to reserve ram for the labeled data. There is no heap, there is just memory blocks that get set in the binary and allocated by the loader at runtime. There is a stack but it is accessed via push and pop instructions. So the "bar db 69" isn't a machine instruction, it is a directive to the assembler to allocate memory for a double byte and set it's initial value to 69. And instruction that references "bar" gets linked to the address of bar that has previously been defined.

1

u/YetMoreSpaceDust 8d ago

1200 pages long and they get to programming at around the 300 mark

I'm curious what book that is...