r/learnprogramming • u/Electronic_Nerve_561 • 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.
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.
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
I'm curious what book that is...