Lab 4
Lab 4 : Introduction to MASM
OBJECTIVE
To familiar with Assembly Language programming tools.
PRELAB
Basic Commands of MASM and dos-Box
Background Information
EDITOR:
An editor is a program, which allows you to create a file containing the assembly language statements for your program. As you type in your program, the editor stores the ASCII codes for the letters and numbers in successive RAM locations. When you have typed in all of your programs, you then save the file on a floppy of hard disk. This file is called source file. The next step is to process the source file with an assembler. In the MASM /TASM assembler, you should give your source file name the extension, .ASM
ASSEMBLER:
An assembler program is used to translate the assembly language mnemonics for instructions to the corresponding binary codes. When you run the assembler, it reads the source file of your program the disk, where you saved it after editing on the first pass through the source program the assembler determines the displacement of named data items, the offset of labels and pails this information in a symbol table. On the second pass through the source program, the assembler produces the binary code for each instruction and inserts the offset etc that is calculated during the first pass. The assembler generates two files on floppy or hard disk. The first file called the object file is given the extension. OBJ. The object file contains the binary codes for the instructions and information about the addresses of the instructions. The second file generated by the assembler is called assembler list file. The list file contains your assembly language statements, the binary codes for each instructions and the offset for each instruction. In MASM/TASM assembler, MASM/TASM source file name ASM is used to assemble the file. Edit source file name LST is used to view the list file, which is generated, when you assemble the file.
LINKER:
A linker is a program used to join several object files into one large object file and convert to an exe file. The linker produces a link file, which contains the binary codes for all the combined modules. The linker however doesn‟t assign absolute addresses to the program, it assigns is said to be reloadable because it can be put anywhere in memory to be run. In MASM/TASM, LINK/TLINK source filename is used to link the file.
DEBUGGER:
A debugger is a program which allows you to load your object code program into system memory, execute the program and troubleshoot are debug it the debugger allows you to look at the contents of registers and memory locations after your program runs. It allows you to change the contents of register and memory locations after your program runs. It allows you to change the contents of register and memory locations and return the program. A debugger also allows you to set a break point at any point in the program. If you inset a breakpoint the debugger will run the program upto the instruction where the breakpoint is set and stop execution. You can then examine register and memory contents to see whether the results are correct at that point. In MASM/TASM, td filename is issued to debug the file.
MASM Commands:
C :/> cd foldername
C:/foldername>edit filename.asm
After this command executed in command prompt an editor window will open. Program should be typed in this window and saved.
DATA SEGMENT ; Define the Data if Required..
NUM1 DW 23C5H
NUM2 DW 6789H
DATA ENDS
CODE SEGMENT ; Code Start Here
ASSUME CS: CODE, DS: DATA, ES: EXTRA; Given Name to the Segment Registers.
START: MOV AX,DATA
MOV DS,AX
MOV AX,EXTRA
MOV ES,AX
SUB AX,AX
MOV AX, NUM1
MOV BX, NUM2
ADD AX,BX
MOV RESULT, AX
INT 3H
CODE ENDS ; Program ends here
END START
To run the program, the following steps have to be followed:
C:/foldername>masm filename.asm
After this command is executed in command prompt if there are no errors in program regarding to syntax the assembler will generates an object module as discuss above.
C:/foldername>link filename.obj
After verifying the program for correct syntax and the generated object files should be linked together. For this the above link command should be executed and it will give an EXE file if the model directive is small as discuss above.
C:/foldername>TD filename.exe
After generating EXE file by the assembler it’s the time to check the output. For this the above command is used and the execution of the program can be done in different ways.
EXERCISES
1) Write ALP for the Data Transfer between registers.
DATA SEGMENT
NUM1 DW 1234H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ; Initialize data segment
MOV DS, AX ; Move AX to DS
MOV AX, NUM1 ; Load NUM1 into AX
MOV BX, AX ; Move AX to BX (Data Transfer)
; At this point, BX contains the value of NUM1
MOV AH, 4CH ; Terminate program
INT 21H
CODE ENDS
END START
2) Write ALP to ADD two 16 bit numbers.
DATA SEGMENT
NUM1 DW 1234H
NUM2 DW 5678H
RESULT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ; Initialize data segment
MOV DS, AX ; Move AX to DS
MOV AX, NUM1 ; Load NUM1 into AX
ADD AX, NUM2 ; Add NUM2 to AX
MOV RESULT, AX ; Store result in RESULT
MOV AH, 4CH ; Terminate program
INT 21H
CODE ENDS
END START
3) Write ALP Subtract two 16 bit numbers.
DATA SEGMENT
NUM1 DW 5678H
NUM2 DW 1234H
RESULT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX, DATA ; Initialize data segment
MOV DS, AX ; Move AX to DS
MOV AX, NUM1 ; Load NUM1 into AX
SUB AX, NUM2 ; Subtract NUM2 from AX
MOV RESULT, AX ; Store result in RESULT
MOV AH, 4CH ; Terminate program
INT 21H
CODE ENDS
END START