ICT106_Week7_Prac 1
Developing
assembly programs under DOS
ICT 106 Fundamentals of Computer Systems
ICT106_Week7_Prac 2
Assembler
An assembler is a program for translating assembly language into object code;
Popular assemblers for Intel x86 architecture include:
Microsoft Macro Assemble (MASM)
Borland Turbo Assembler (TASM)
Netwide Assembler (NASM)
Microsoft Macro Assemble (MASM)
Originally developed by Microsoft for MS- DOS operating system;
Support a wide variety of macro facilities and structured programming;
Support for 16-bit and 32-bit versions;
The latest version is called MASM32 V9;
MASM 611 version is used for demonstration and lab practices.
Assembling, Linking and Executing programs
The steps involved are:
1. Edit
A source program consisting of assembly language statements is created using a text editor (eg, Programmer’s File Editor - pfe, which is a public domain editor and can be downloaded from the ICT106 Unit materials site). This results in a source file of ASCII text.
2. Assemble
The assembler program reads the source file and produces an object file, which is a machine-language translation of the source program.
ICT106_Week7_Prac 5
3. Link
The object file may contain calls to routines (eg, operating system service functions) in an external link library.
The linker then
copies the needed routines from the link library into the object file,
creates a special header record at the beginning of the program, and
produces an executable program.
ICT106_Week7_Prac 6
4. Load
When the program is run by entering its name on the DOS command line, the DOS loader decodes the header record of the executable program and loads it into memory
5. Execute
The CPU begins executing the program
Hello World program in assembly language
Title Hello World Program
; File: hello.asm
; This program displays “Hello, world!”
.MODEL SMALL ;each of data and code segments
; <=64K
.STACK 100h ;reserve 256 bytes of stack space
; ---data segment ---
.DATA ;indicates beginning of data
; segment
message db “Hello, world!”,0dh,0ah,’$’
; ---code segment ---
.CODE ;indicates beginning of code segment Main PROC ;proc declares the start of a proc
MOV AX, @DATA ;copy the addr. of data MOV DS, AX ;segment into DS register
MOV AH, 9 ;place the number of DOS
;output routine in the AH
;register
MOV DX, offset message ;place address of
;string in DX register INT 21h ;execute the DOS service
;function whose addr. is in AH
MOV AX, 4C00H ;halt the program and INT 21H ;return control to DOS
Main ENDP ;marks the end of current procedure END Main ;the program exit point
ICT106_Week7_Prac 9
Alternate Version
Title Hello World Program
; File: hello.asm
; Alternate program displays “Hello, world!”
.MODEL SMALL ;each of data and code segments
; <=64K
.STACK 100h ;reserve 256 bytes of stack space
; ---data segment ---
.DATA ;indicates beginning of data
; segment
Message BYTE “Hello, World!”,0dh,0ah
ICT106_Week7_Prac 10
; ---code segment ---
.CODE ;indicates beginning of code segment Main PROC ;proc declares the start of a proc
MOV AX, @DATA ;copy the addr. of data MOV DS, AX ;segment into DS register
MOV AH, 40h ;write to file/device
;routine in the AH register MOV BX,1 ;output to console
MOV CX,SIZEOF message ;number of bytes MOV DX, offset message ;place address of
;string in DX register INT 21h ;execute the DOS service
;function whose addr. is in AH
MOV AX, 4C00H ;halt the program and INT 21H ;return control to DOS Main ENDP ;marks the end of current procedure END Main ;the program exit point
MS-DOS Function Calls
The main difference between two versions is on the use of MS-DOS function calls (INT 21h)
INT 21h Function 9: write a $-terminated string to standard output;
Received arguments
AH = 9
DS:DX = offset of the string
INT 21h Function 40h: write an array of bytes to a file or device
Received arguments:
AH = 40h
BX = file or device handle (console = 1)
CX = number of bytes to write
DS:DX = address of array
ICT106_Week7_Prac 13
Hello.c
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
ICT106_Week7_Prac 14
Example
The table below displays a list of file names that would be created if the Hello World program was assembled and linked
Step Outcome Filename
Edit Source program Hello.asm Assembly Object program Hello.obj
Listing File Hello.lst Link Executable program Hello.exe
Map file Hello.map
Compilation
Use masm.exe to compile hello.asm
ICT106_Week7_Prac 17
Linking
Produce the executable file using link.exe
ICT106_Week7_Prac 18
Execution
Compiling with gcc compiler
gcc hello.c –o hello_c.exe
Using strip command
Discard symbols from object files
ICT106_Week7_Prac 21
MSVC++ 6.0
Generate HelloWorld.exe using MSVC
ICT106_Week7_Prac 22
Decremental Counter
Title Decremental timer
.MODEL SMALL ;each of data and code segments <=64K .STACK 100h ;reserve 256 bytes of stack space
; ---data segment ---
.DATA ;indicates beginning of data segment count EQU 65535 ;initialise counter
count2 EQU 10000 ;second counter Message db "Final result is: $" ;output string
; ---code segment ---
.CODE ;indicates beginning of code segment Main PROC ;proc declares the start of a proc
MOV AX, @DATA ;copy the addr. of data MOV DS, AX ;segment into DS register MOV bx, count ;load counter to BX register L1: MOV ax, count2
L2: DEC ax JNZ L2
DEC bx ;decrement counter by 1 JNZ L1 ;Jump to L1 if BX is not zero MOV AH, 9 ;place the number of DOS output
;routine in the AH register MOV DX, offset message ;place address of
INT 21H ;execute the DOS service
;function whose addr. is in AH MOV ah, 6 ;write a character to standard output ADD bl, 48 ;convert integer to character MOV dl, bl ;output character value INT 21H ;execute the DOS service MOV AX, 4C00H ;halt the program and INT 21H ;return control to DOS
Main ENDP ;marks the end of current procedure END Main ;the program exit point
ICT106_Week7_Prac 25
Equivalent C Code
int main() {
int count = 65535, count2=10000;
while(count>0) { count2 = 10000;
while(count2>0) { count2--;
} count--;
}
printf("Final result is: %d", count);
return 0;
}
ICT106_Week7_Prac 26
Unsing ML command
Type “ML /Fl count.asm” to complete compilation and linking in a single step
Note that it is case sensitive
Check the size of two executable files
Timing Command
time is a simple command or give resource usage in Unix/Linux.
Syntax:
time executable file
Output:
Real: Wall clock time
User: User CPU time
Sys: System CPU time
ICT106_Week7_Prac 29 ICT106_Week7_Prac 30
Summary of file size and execution
time
Program Size Timehello.asm 911 bytes
hello.c 82 bytes
hello.exe 546 bytes
hello_c.exe 8873 bytes HelloWorld.exe 155748 bytes
count.asm 1283 bytes
count.c 245 bytes
count.exe 568 bytes 0.751sec
Count_c.exe 8873 bytes 1.923sec