Doing time traveling with assembly language
I’m taking a distance course at the University in Ume? learning about Linux for development (https://www.umu.se/utbildning/kurser/linux-som-utvecklingsmiljo-1/). The course is covering a lot and it’s been big fun to do the exercises and the teachers are great. Some exercises is about coding in C and some involve debugging. Anyway, firing off the debugger inspired me to dust off my old assembler “skills”.
My first memories from programming was in the early 80’s. Doing BASIC programming on a computer at school. Later on I got my 8-bit ZX Spectrum with 48K memory (https://en.wikipedia.org/wiki/ZX_Spectrum), but it all really started with my first IBM PC. It was a Model 5155 – a “portable” one weighing over 13 kilos (https://en.wikipedia.org/wiki/IBM_Portable_Personal_Computer). No hard drive, but two floppy drives, and it was on this setup I learned 16-bit 8088 assembly language 35 years ago. I still have the book I started to learn from.
Of course it’s an understatement to say that a lot has happened since the mid 80’s. It’s more than “a lot”. It amazes me when I reflect on the evolution of IT and computer hardware.
Me in Boca Raton, Florida in the early 90s
But let’s focus on one of the smallest building blocks. The CPU. This is how I briefly summarize the journey.
The Intel 8088 was a 16-bit CPU with four 16bit registers (AX, BX, CX, DX) and three segment registers (CS, DS, SS). Then the 80286 came, still 16-bit, and with the 80386 (i386) we entered the 32-bit world. At that time I ran OS/2, and developers talked about “thunking” and how to mix 16-bit and 32-bit code. A phenomenon that actually was well-known by the mainframe community who had been doing it in OS/370 (OS/390 and z/OS) since the 70s, and they wondered about all the fuzz about it. Then the 80486 (i486) came. It was still 32-bit, and the same with Pentium (IA-32). Now we are on a 64-bit architecture with the IA-64 (X86-64).
Going back in time to IBM PC DOS 3.3 (mid 80’s)
There are many ways to run 16-bit DOS programs, and I chose to install it in a virtual machine. Booting the diskette, and installing the system on a virtual hard drive. Welcome back to the 80’s!
I installed Microsoft Macro Assembler from 1985 and wrote my first 16-bit assembler program in many years. Just a simple “hello world”. As you can see below the executable (.EXE) is 544 bytes. Compact stuff, and it actually prints the hello world message. Success
IBM DOS 3.3
Above you can see the HELLO.EXE file, but I also created the H1.COM, which is another kind of executable. The .EXE file is the standard executable format, but there is also the more compact COM file format. The COM file format was inherited from the CP/M operating system which was built in the 70s.
Hello world as an .EXE file
This is how my small program looks. Hello world is defined as msg and located in the data segment. In the code, I first set up the data segment. Then the message is printed and at the end the program is terminated.
.model small
.stack 100H
.data
msg db 'Hello World', 0dh, 0ah, '$'
.code
main proc
mov ax,@data
mov ds,ax
lea dx, msg
mov ah,9
int 21h
mov ah,4ch
int 21h
main endp
end main
The .exe file is created by running masm and link, like illustrated below.
领英推荐
Running masm and link
Hello World as a .COM file
The COM file is more or less the same. Since it only contains one segment, CS, DS, ES and SS points to the same segment, and the actual message is in the code segment.
_TEXT SEGMENT
ASSUME CS:_TEXT, DS: _TEXT, ES: _TEXT, SS:_TEXT
ORG 100H
START:
mov ah,09h
mov dx, offset msg
int 21h
mov ah,4ch
int 21h
msg db "hello world", 0dh,0ah,'$'
_TEXT ENDS
end START
There is one extra step in building the COM file compared to building the EXE. I’m calling masm and link in the same way, but the linker gives a warning that there is no stack segment.
Building the COM file
That’s where the exe2bin utility comes in, and then the h1.com file is created.
Building the COM file
So what did I learn?
For me it is important to look back at history in order to learn for the future. Back in the 80’s, memory was expensive and the first PCs had at maximum 640 KB memory. Just a single photo today is at least 10 times bigger than that. So of course it is interesting to reflect about constraints and limitations.
Also, wasn’t it Bill Gates who said?
640K ought to be enough for anyone.
This blog post obviously exposes the nerd in me, but I think it is important in my professional life to have the ability to both go deep, but also wide. To see the whole picture, but also the details.
I am by no means an experienced assembler programmer, but I found this to be so fun, so stay tuned for when I explore the 64-bit world(s). My wife asked me about my definition of fun, so I guess we are driven by different things.
More later, and take care!
Technology oriented IT Architect - Digital Workplace
3 年Thanks for the time travel… a tip check out the Int 10h interupt call for doing graphics in VGA ”mode 13h” that old DOS games used https://en.m.wikipedia.org/wiki/Mode_13h
IT-Strategist at Stockholms Stadsledningskontor | IT Architect | Chief Integration Officer
3 年Assembly programming was my best skill in the 1980 th. I did create a disassembler for my Amiga 500 :). Useful when clearing intros for Amiga games. Good luck with the course ??
Thanks for this time travel. Very thought-provoking! Looking forward to the next episode. Maybe VIC-20 will appear ??
Project manager/enterprise architect with focus on results through teamwork (retired)
3 年Really interesting read, and my how time flies.
IT Systems & Project Manager | Scrum Master | SAFe? 6 Agilist | Cloud | eCommerce | Design Thinking | I help companies empower teams to deliver real business value
3 年Thanks for this little trip down memory lane! ??