Sample Assembly Programs

It's time to look at some simple assembly language programs:


Program #1:

;*************************************************
; Purpose: To show simple addition and subtraction
;          using 32-bit registers
;*************************************************

TITLE AddSub1.asm - Protected Mode

INCLUDE Irvine32.inc

.code
main PROC

	mov eax, 2h		; EAX = 2h
	add eax, 3h		; EAX = 3h
	sub eax, 1h		; EAX = 4h
	call DumpRegs

	exit
main ENDP
END main

We can invoke the assembler to assemble the above 32-bit program as follows:

make32 AddSub1
If no errors exist, this produces an executable called AddSub1.exe. Executing the program will dump the registers EAX, EBX, ECX, EDX, ESI, EDI, EBP, ESP, EIP, EFL, CF SF ZF OF.

So what might a 16-bit program to do the same thing look like?


Program #2:

;*************************************************
; Purpose: To show simple addition and subtraction
;          using 16-bit registers and a data 
;          segment
;*************************************************

INCLUDE Irvine16.inc

.data
v1     dword  2h
v2     dword  4h
v3     dword  1h
rslt   dword  ?

.code
main PROC
          mov ax, @data    ; place the address of data seg in ax
          mov ds, ax       ; initialize DS to address of data seg
          mov eax,v1       ; place 2h in eax
          add eax,v2       ; add 4h to eax
          sub eax,v3       ; subtract 1h from eax
          mov rslt,eax     ; store the result (5h)
          call DumpRegs    ; display the registers

          exit
main ENDP
END main
Between now and the next time we get together, get the 16-bit version of this program running using Visual Studio 6.0. Here are the steps:

  1. Copy and paste the program into a text file using Visual Studio 6.0. Make sure the name ends with a .asm extension.
  2. Copy Irvine16.inc and Irvine16.lib to the same location as the text file. Both of these files can be found on the CD that came with your book.
  3. Under Tools, find the Assemble 16-bit (Real Mode) option and assemble the program.
  4. If the program assembled with zero errors, then find the Run 16-bit (Real Mode) option and execute the program.