Slash Boxes

SoylentNews is people

SoylentNews is powered by your submissions, so send in your scoop. Only 18 submissions in the queue.
posted by NCommander on Tuesday August 30 2016, @12:14PM   Printer-friendly
from the int-21h-is-how-cool-kids-did-it dept.

I've made no secret that I'd like to bring original content to SoylentNews, and recently polled the community on their feelings for crowdfunding articles. The overall response was somewhat lukewarm mostly on dividing where money and paying authors. As such, taking that into account, I decided to write a series of articles for SN in an attempt to drive more subscriptions and readers to the site, and to scratch a personal itch on doing a retro-computing project. The question then became: What to write?

As part of a conversation on IRC, part of me wondered what a modern day keylogger would have looked running on DOS. In the world of 2016, its no secret that various three letter agencies engage in mass surveillance and cyberwarfare. A keylogger would be part of any basic set of attack tools. The question is what would a potential attack tool have looked like if it was written during the 1980s. Back in 1980, the world was a very different place both from a networking and programming perspective.

For example, in 1988 (the year I was born), the IBM PC/XT and AT would have been a relatively common fixture, and the PS/2 only recently released. Most of the personal computing market ran some version of DOS, networking (which was rare) frequently took the form of Token Ring or ARCNet equipment. Further up the stack, TCP/IP competed with IPX, NetBIOS, and several other protocols for dominance. From the programming side, coding for DOS is very different that any modern platform as you had to deal with Intel's segmented architecture, and interacting directly with both the BIOS, and hardware. As such its an interesting look at how technology has evolved since.

Now obviously, I don't want to release a ready-made attack tool to be abused for the masses especially since DOS is still frequently used in embedded and industry roles. As such, I'm going to target a non-IP based protocol for logging both to explore these technologies, while simultaneously making it as useless as possible. To the extent possible, I will try and keep everything accessible to non-programmers, but this isn't intended as a tutorial for real mode programming. As such I'm not going to go super in-depth in places, but will try to link relevant information. If anyone is confused, post a comment, and I'll answer questions or edit these articles as they go live.

More past the break ...

Looking At Our Target

Back in 1984, IBM released the Personal Computer/AT which can be seen as the common ancestor of all modern PCs. Clone manufacturers copied the basic hardware and software interfaces which made the AT, and created the concept of PC-compatible software. Due to the sheer proliferation of both the AT and its clones, these interfaces became a de-facto standard which continues to this very day. As such, well-written software for the AT can generally be run on modern PCs with a minimum of hassle, and it is completely possible to run ancient versions of DOS and OS/2 on modern hardware due to backwards compatibility.

A typical business PC of the era likely looked something like this:

  • An Intel 8086 or 80286 processor running at 4-6 MHz
  • 256 kilobytes to 1 megabyte of RAM
  • 5-20 MiB HDD + 5.25 floppy disk drive
  • Operating System: DOS 3.x or OS/2 1.x
  • Network: Token Ring connected to a NetWare server, or OS/2 LAN Manager
  • Cost: ~$6000 USD in 1987

To put that in perspective, many of today's microcontrollers have on-par or better specifications than the original PC/AT. From a programming perspective, even taking into account resource limitations, coding for the PC/AT is drastically different from many modern systems due to the segmented memory model used by the 8086 and 80286. Before we dive into the nitty-gritty of a basic 'Hello World' program, we need to take a closer look at the programming model and memory architecture used by the 8086 which was a 16-bit processor.

Real Mode Programming

If the AT is the common ancestor of all PC-compatibles, then the Intel 8086 is processor equivalent. The 8086 was a 16-bit processor that operated at a top clock speed of 10 MHz, had a 20-bit address bus that supported up to 1 megabyte of RAM, and provided fourteen registers. Registers are essentially very fast storage locations physically located within the processor that were used to perform various operations. Four registers (AX, BX, CX, and DX) are general purpose, meaning they can be used for any operation. Eight (described below) are dedicated to working with segments, and the final registers are the processor's current instruction pointer (IP), and state (FLAGS) An important point in understanding the differences between modern programming environments and those used by early PCs deals with the difference between 16-bit and 32/64-bit programming. At the most fundamental level, the number of bits a processor has refers to the size of numbers (or integers) it works with internally. As such, the largest possible unsigned number a 16-bit processor can directly work with is 2 to the power of 16 (minus 1) or 65,535. As the name suggests, 32-bit processors work with larger numbers, with the maximum being 4,294,967,296. Thus, a 16-bit processor can only reference up to 64 KiB of memory at a given time while a 32-bit processor can reference up to 4 GiB, and a 64-bit processor can reference up to 16 exbibytes of memory directly.

At this point, you may be asking yourselves, "if a 16-bit processor could only work with 64 KiB RAM directly, how did the the 8086 support up to 1 megabyte?" The answer comes from the segmented memory model. Instead of directly referencing a location in RAM, addresses were divided into two 16-bit parts, the selector and offset. Segments are 64 kilobyte selections of RAM. They could generally be considered the computing equivalent of a postal code, telling the processor where to look for data. The offset then told the processor where exactly within that segment the data it wanted was located. On the 8086, the selector represented the top 16-bits of an address, and then the offset was added to it to create 20-bits (or 1 megabyte) of addressable memory. Segments and offsets are referenced by the processor in special registers; in short you had the following:

  • Segments
    • CS: Code segment - Application code
    • DS: Data segment - Application data
    • SS: Stack segment - Stack (or working space) location
    • ES: Extra segment - Programmer defined 'spare' segment
  • Offsets
    • SI - Source Index
    • DI - Destination Index
    • BP - Base pointer
    • SP - Stack pointer

As such, memory addresses on the 8086 were written in the form of segment:offset. For example, a given memory address of 0x000FFFFF could be written as F000:FFFF. As a consequence, multiple segment:offset pairs could refer to the same bit of memory; the addresses F555:AAAF, F000:FFFF, and F800:7FFF all refer to the same bit of memory. The segmentation model also had important performance and operational characteristics to consider.

The most important was that since data could be within the same segment, or a different type of segment, you had two different types of pointers to work with them. Near pointers (which is just the 16-bit offset) deal with data within the same segment, and are very fast as no state information has to be changed to reference them. Far pointers pointed to data in a different selector and required multiple operations to work with as you had to not only load and store the two 16-bit components, you had to change the segment registers to the correct values. In practice, that meant far pointers were extremely costly in terms of execution time. The performance hit was bad enough that it eventually lead to one of the greatest (or worst) backward compatibility hacks of all time: the A20 gate, something which I could write a whole article on.

The segmented memory model also meant that any high level programming languages had to incorporate lower-level programming details into it. For example, while C compilers were available for the 8086 (in the form on Microsoft C), the C programming language had to be modified to work with the memory model. This meant that instead of just having the standard C pointer types, you had to deal with near and far pointers, and the layout of data and code within segments to make the whole thing work. This meant that coding for pre-80386 processors required code specifically written for the 8086 and the 80286.

Furthermore, most of the functionality provided by the BIOS and DOS were only available in the form of interrupts. Interrupts are special signals used by the process that something needs immediate attention; for examine, typing a key on a keyboard generates a IRQ 1 interrupt to let DOS and applications know something happened. Interrupts can be generated in software (the 'int' instruction) or hardware. As interrupt handling can generally only be done in raw assembly, many DOS apps of the era were written (in whole or in part) in intel assembly. This brings us to our next topic: the DOS programming model

Disassembling 'Hello World'

Before digging more into the subject, let's look at the traditional 'Hello World' program written for DOS. All code posted here is compiled with NASM

; Hello.asm - Hello World

section .text
org 0x100

 mov ah, 9
 mov dx, str_hello
 int 0x21

section .data
str_hello: db "Hello World",'$'

Pretty, right? Even for those familiar with 32-bit x86 assembly programming may not be able to understand this at first glance what this does. To prevent this from getting too long, I'm going to gloss over the specifics of how DOS loads programs, and simply what this does. For non-programmers, this may be confusing, but I'll try an explain it below.

The first part of the file has the code segment (marked 'section .text' in NASM) and our program's entry point. With COM files such as this, execution begins at the top of file. As such, _entry is where we enter the program. We immediately execute two 'mov' instructions to load values into the top half of AX (AH), and a near pointer to our string into DX. Ignore 9 for now, we'll get to it in a moment. Afterwords, we trip an interrupt, with the number in hex (0x21) after it being the interrupt we want to trip. DOS's functions are exposed as interrupts on 0x20 to 0x2F; 0x21 is roughly equivalent to stdio in C. 0x21 uses the value in AX to determine which subfunction we want, in this case, 9, to write to console. DOS expects a string terminated in $ in DX; it does not use null-terminated strings like you may expect. After we return from the interrupt, we simply exit the program by calling ret.

Under DOS, there is no standard library with nicely named functions to help you out of the box (though many compilers did ship with these such as Watcom C). Instead, you have to load values into registers, and call the correct interrupt to make anything happen. Fortunately, lists of known interrupts are available to make the process less painful. Furthermore, DOS only provides filesystem and network operations. For anything else, you need to talk to the BIOS or hardware directly. The best way to think of DOS from a programming perspective is essentially an extension of the basic input/output functionality that IBM provided in ROM rather than a full operating system.

We'll dig more into the specifics on future articles, but the takeaway here is that if you want to do anything in DOS, interrupts and reference tables are the only way to do so.


As an introduction article, we looked at the basics of how 16-bit real mode programming works and the DOS programming model. While something of a dry read, it's a necessary foundation to understand the basic building blocks of what is to come. In the next article, we'll look more at the DOS API, and terminate-and-stay resident programs, as well as hooking interrupts.

This discussion has been archived. No new comments can be posted.
Display Options Threshold/Breakthrough Mark All as Read Mark All as Unread
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
  • (Score: 2) by NCommander on Tuesday August 30 2016, @06:07PM

    by NCommander (2) Subscriber Badge <> on Tuesday August 30 2016, @06:07PM (#395377) Homepage Journal

    TSRs let you do ungodly things if you knew what you were doing. My dad used to use Reference Manager for DOS which would hook into WordPerfect by hitting SysRq and pop up a UI to put references right in the application. Refman for Windows never worked anywhere as well.

    WordPerfect for DOS was probably one of the best editors I've ever used. Still haven't found anything I liked as much.

    Still always moving
    Starting Score:    1  point
    Karma-Bonus Modifier   +1  

    Total Score:   2  
  • (Score: 2) by iWantToKeepAnon on Thursday September 01 2016, @05:09PM

    by iWantToKeepAnon (686) on Thursday September 01 2016, @05:09PM (#396265) Homepage Journal
    Since you seem to be in the mood for nostalgia, have you read this : []
    "Happy families are all alike; every unhappy family is unhappy in its own way." -- Anna Karenina by Leo Tolstoy
    • (Score: 2) by Reziac on Friday September 16 2016, @04:47AM

      by Reziac (2489) on Friday September 16 2016, @04:47AM (#402622) Homepage

      Thanks for the link. I'd forgotten it was online. I have a hardcopy in one of these library boxes -- I sorta collect WordPerfect stuff.

      And there is no Alkibiades to come back and save us from ourselves.
  • (Score: 2) by Reziac on Friday September 16 2016, @05:12AM

    by Reziac (2489) on Friday September 16 2016, @05:12AM (#402628) Homepage

    You're the same age as my trusty and beloved 286. Which I still have. :)

    I was still using WPDOS5.1+ for everyday up until about 5 years ago. RoughDraft finally weaned me off it in WinXP (better multifile search function, better HTML export, tho with the usual limitations of RTF) but I still use WPDOS for some work, and collect old versions and books and stuff (at least when it's cheap enough). There's nothing else quite like it for competent, compact, efficient, and as close to bug-free as complex software gets. You know it was written in assembly? Unfortunately I've been told by a Corel rep that the source code has been lost. :(

    And there is no Alkibiades to come back and save us from ourselves.
    • (Score: 2) by NCommander on Friday September 16 2016, @05:39AM

      by NCommander (2) Subscriber Badge <> on Friday September 16 2016, @05:39AM (#402632) Homepage Journal

      My very first computer was a hand-me-down Premium Exec AST/386 laptop with 4 MiB of RAM, DOS 6.0, and Windows 3.1. Primarily apps I used on that thing was Windows 3.1, ProComm Plus, and WordPerfect.

      I primarily used WordPerfect 6.x at the time, as it had a WYISWYG, and had better printer compatibility. Damn thing weighted 12 pounds but worked really really well for editing and such. I don't think I could use WP 5.1 these days or similar due to low resolution; until I switched to using Linux primarily, I used WordPerfect for Windows, which while not "great", at least had the all important and magical reveal codes feature.

      I just finished coding up the last section of the example code and have most of the article written (scheduled to go out on Monday; I try not to do major things on Friday or the weekend). Ran into a compatibility quirk on FreeDOS that I had to patch around (DOSBox sets default handler for all interrupts, FreeDOS doesn't, so trying to chain blindly cases a GPF. I don't have a copy of MS-DOS or DR-DOS to confirm their functionality). If you want to grab the current TSR code and run it to check to see what actual period-specific DOS does, I'll throw a credit to you in the next article.

      Still always moving
      • (Score: 2) by Reziac on Friday September 16 2016, @07:01PM

        by Reziac (2489) on Friday September 16 2016, @07:01PM (#402903) Homepage

        I don't offhand see the link to the code (I assume it's compiled, I don't have any tools handy and hardly any knowledge of how to use 'em anyway), and I'd have to find the 286 an ISA vidcard before it could be used (assuming there's even two bytes of RAM not already in use, I had it really packed solid with TSRs), it has MSDOS6.00, but come to think of it, the 386 laptop is here somewhere (I swear I've seen it since I moved) and it has MSDOS5 or 6. My everyday DOS machine is MSDOS7, tho the only real difference is that it groks FAT32. I've never seen it behave any different otherwise, and all the external utils are interchangeable.

        People like to bitch about DOS, but that 286 had multiple uptimes of ~2 years in heavy use, and only ever got restarted (once I'd ID'd and locked out the bad memory chip) when that old MFM HD needed a fresh low-level format. Tho after I added a fan blowing directly on the HD, that problem went away, which oughta tell us something.

        And there is no Alkibiades to come back and save us from ourselves.
        • (Score: 2) by NCommander on Saturday September 17 2016, @05:29AM

          by NCommander (2) Subscriber Badge <> on Saturday September 17 2016, @05:29AM (#403034) Homepage Journal

          The code and binaries are linked from my journal for the next article (which is set to go live on Monday: []

          Testing on real DOS would be rather nifty. I also won't mind seeing if it worked on DR-DOS or such. As written, the code should work on anything DOS 2.x+ or above as of right now.

          Still always moving
          • (Score: 2) by Reziac on Saturday September 17 2016, @06:30AM

            by Reziac (2489) on Saturday September 17 2016, @06:30AM (#403045) Homepage

            DRDOS can be persnickety, especially its protected-mode memory manager. Had to beat it with sticks to get DOOM to run, back in the day. On my old Win31 and 9x boxen I used a sort of MSDOS/DRDOS hybrid with parts of both. Eventually gave up on DRDOS as it's both slower (by about 10%, IIRC) and compared to MSDOS, rather buggy. I'd have to root through the pile of boot disks to see if I've got DRDOS in there. Old versions of Partition Magic came with a DRDOS boot disk.

            I probably have an MSDOS 3.2 boot disk somewhere, gods know which box.

            I preferred MSDOS 6.00 (and its younger sibling 7.x) to 6.2x, which had a couple bugs 6.0/7.x lack (IIRC in 6.22 you could hang FORMAT with one of the abort options).

            And there is no Alkibiades to come back and save us from ourselves.
            • (Score: 2) by NCommander on Saturday September 17 2016, @07:15AM

              by NCommander (2) Subscriber Badge <> on Saturday September 17 2016, @07:15AM (#403054) Homepage Journal

              What I'm mostly trying to figure out is if MS-DOS installs a default TSR for all interrupts. Basically, calling an interrupt blindly on FreeDOS will cause the processor to fault because the IVT points to 0000:0000 as a default entry if there's no TSR sitting on it. DOSBox on the other hand installs a default handler to F000:xxxx which is where the DOS kernel would live and appears just to do iret.

              Protected mode in general is a bitch on DOS since you have to do a lot of setup to switch to and from. From real mode, to enter protected mode, you have to:

                * Disable interrupts
                * Setup the GDT
                * Setup a 32-bit IDT
                * Configure segments/paging
                * Throw the protected bit
                * Far call into protected mode call
                * Re-enable interrupts
                * Thunk interrupts from protected mode to real mode.
                    * If necessary to call a real mode interrupt (i.e. BIOS), you have to leave protected mode back to real mode, then do the above all over again.

              Not very surprising an entire cottage industry sprouted on making DOS extenders for doing all this insanely. On the 80286 it was worse since you have to fault or reset the processor to kick back into protected mode since protected mode was believed to be the future, and backwards compatibility wasn't really necessary for protected mode OSes. (the expectation was OS/2 would replace DOS entirely).

              Unreal mode primarily got used since you got the advantages of more memory space (since you could put the segment registers above 1 MiB via LOADALL or a protected/real mode magic), and none of the disadvantages. Generally you could get code to fit with in 640 KiB (even today that's generally true) without too much work. You could do Unreal huge mode to move CS high though that had its own set of pain to deal with; I can't even find an example that does so under DOS.

              Still always moving