Ahmed Kortas
Ahmed Kortas

Reputation: 41

Can anyone help me understand the directives and instructions from a small 16-bit DOS .COM program?

I'm learning assembly and I would like some help. If anyone can help me understand this pretty basic piece of code I would be grateful.

        org 100h; directive for compiler
        cpu 8086; cpu architecture 
        jmp START; this will jump the address of START from what i understood
BYTE1:  db 32; define byte 32 not sure what it does to be honest
TEXT1:  db 'Hardwarenahes Programmieren'; define a double byte
        times 4 db 0,'1';??? 
WORT1:  dw 1,2,3,4,1234 ;Define Word ???
        dd 1234h';??
START:  mov bx, BYTE1;move value if byte1 to bx register
WDH:    mov al, [bx]; pointer for bx register to move value into al register
        out 0, al;?
        inc bx;increment value of bx
        jmp WDH; jump to WDH and execute again 

Upvotes: 1

Views: 266

Answers (1)

Kai Burghardt
Kai Burghardt

Reputation: 1600

        org 100h; directive for compiler

The word “compiler” is inadequate: Assembly programs are not “compiled”. You say compilation if you are translating from a more abstract programming language to a more concrete “programming” language. For instance Java → Java Bytecode.

Assembly language → machine code is not considered “compilation”, because both “languages” have the same “power”, are both as capable in describing an algorithm as the other.

Documentation of the org directive.

        cpu 8086; cpu architecture

The cpu directive restricts the set of available instructions. In the following code the nasm(1) will, for instance, refuse to assemble the cpuid instruction.

        jmp START; this will jump the address of START from what i understood

Yes. It is used here, because in the flat bin format there are no sections. That means, there is no indication “this is data” vs. “this is (instruction) code”. From the processor’s point of view everything is code. Jumping past the subsequent data is important, because otherwise the processor would interpret the following data values as instructions.

BYTE1:  db 32; define byte 32 not sure what it does to be honest

There should have been a comment explaining this. In ASCII, 32 is the decimal value of the space character (' ').

TEXT1:  db 'Hardwarenahes Programmieren'; define a double byte

I believe the d in db stands for datum/data (I never saw a definite source claiming this). The d… pseudo-instructions define (explicitly initialized) data.

        times 4 db 0,'1';??? 

This is shorthand for db 0, '1', 0, '1', 0, '1', 0, '1'. Possibly 0 means (if written to an appropriate device) “make a newline”, and (on IBM mainframes) writing '1' to the first column of a line means “advance to next page” (≈ form feed).

WORT1:  dw 1,2,3,4,1234 ;Define Word ???
        dd 1234h';??

The suffixes are (excerpt):

suffix stands for size
b byte 8‑bit quantity
w word 16‑bit quantity
d double word  32‑bit quantity
q quad-word  64‑bit quantity

The trailing h to the integer constant indicates a hexadecimal base.

START:  mov bx, BYTE1;move value if byte1 to bx register

BYTE1 is a label. A label is a symbolic representation of an address. This line loads the address that BYTE1 represents into register bx.

WDH:    mov al, [bx]; pointer for bx register to move value into al register

I think you understood this correctly, but your comment is a bit confusing, so I’ll rather write it out:

  • [bx] means take the bx register and interpret it as a memory address.
  • al is the lower half of the ax register. (ah would be the higher half.)
  • mov paired with a memory operand as source, and an 8‑bit register as destination copies the contents found at the source into the destination.
        out 0, al;?

On , hardware is accessed essentially in an array-like fashion. Here 0 is the index in this array and we write the contents of al to port 0.
It would make sense if port 0 was connected to some kind of output device (printer/screen), but the meaning of the port 0 depends on the specific hardware configuration.

        inc bx;increment value of bx
        jmp WDH; jump to WDH and execute again 

Yes. From the German text I can suppose WDH stands for „wiederholen“ (repeat).

PS: Maybe the book/tutorial you’re reading is too old? (Advancing to the next [printed] page by writing '1' to the first column?) There are some resources at the x86 tag info page.

Upvotes: 4

Related Questions