This blog was last modified 474 days before.

Software Interrupt

8086 x16 ASM allows you to produce Software Interrupt. Thie name may be frustrated but it actually can do a lots of useful things including:

  • Output char/string to stdout.
  • Read char/string from keyboard buffer.
  • Get system time.
  • etc.

Check this 8086 Software Interrupt Reference for more info.

EMU8086 Lib

If you are using EMU8086, then you can make use of its built-in library which provide some practical functoin and macros. Check How To Use emu8086.inc for more info.

Multi Segments

You can create segments like below:

<segment_name> SEGMENT
; segment content here
ENDS

There are some special segment name:

  • code
  • stack
  • data

IP register will store the start address of code segment. DS register will store the start address of data segment. SS:[SP] will be the base address of stack.

Notice that because stack works from large address to small address, so SS:[SP] is actually where your stack segment end if you browse the memory from small address to large address. See image below:

image.png

You may also notice that the physical address of these segments are related to where they are written in the code.

At least in emu8086, the order of segment physical address corresponding to the order they are written in our source asm code.

Recommend Practice

Since the stack is used from upper address to lower, we recommend you put the stack segment as the first one when you writing multi-segment program. Or you can reserve enough space for your stack like below:

stack segment
dw 128 dup(0)
ends

This will reserve 128 16-bit memory for stack with all memory units set to 0 at the beginning.

Actually when you do not use multi-segment schemes and just directly write instruction line by line in asm file like below:

mov ax, 1234h
mov bx, ax
push bx
pop bx
ret

Then you will find that your default stack base address (which is the value in SS) is always the same as the address of your code segment (which is the value in CS).

image.png

What does this mean? This means that in this case, the memory of your stack segment is immediately in front of the code segment.

image.png

And by this default act, the program could prevent the code segment being accessed/overwrited accidentally when you are pushing something your stack.

Return Control To OS In Multi-Segment Program

Anyway the issue memtion above is directly related to the behaviour of ret. Any usually when you write multi-segment asm, you will not use ret to mark the end of your program and return back to control. Instead, you will use something we have introduced above: Software Instruction.

See the example asm template in below:

; multi-segment executable file template.

data segment
    ; add your data here!
    pkey db "press any key...$"
ends

stack segment
    dw   128  dup(0)
ends

code segment
start:
    ; your code here!
    
    mov ax, 4c00h ; exit to operating system.
    int 21h    
ends

end start ; set entry point and stop the assembler.

Notice that we use the code below to interrput our program.

mov ax, 4c00h
int 21h

You can see the description in the manual:

INT 21h / AH=4Ch - return control to the operating system (stop program).

So actually a mov ah 4ch would also work.