You are on page 1of 13

• TITLE Program Template (Template.

asm)
• ; Program Description:
• ; Author:
• ; Creation Date:
• ; Revisions:
• ; Date:
• .data
• ; (insert variables here)
• .code
• main PROC
• ; (insert executable instructions here)
• exit
• main ENDP
• ; (insert additional procedures here)
• END main
• The Assemble-Link-Execute Cycle
• The process of editing, assembling, linking, and executing assembly
language programs is summarized
• Step 1:
A programmer uses a text editor to create an ASCII text file
named the source file.
• Step 2:
The assembler reads the source file and produces an object file,
a machine-language translation of the program. Optionally, it produces
a listing file.
If any errors occur, the programmer must return to Step 1 and fix the
program
• Step 3:
The linker reads the object file and checks to see if the program contains
any calls to procedures in a link library.
The linker copies any required procedures from the link library, combines
them with the object file, and produces the executable
file.
• Step 4:
The operating system loader utility reads the executable file into memory
and branches the CPU to the program’s starting address, and the program
begins to execute.
. DATA
prompt_msg1 DB 'Please input the first number: ',0
prompt_msg2 DB 'Please input the second number: ',0
sum_msg DB 'The sum is ',0
. CODE
16: main : PROC
17. STARTUP
18: PutStr prompt_msg1 ; request first number
19: GetInt CX ; CX := first number
20: nwln
21: PutStr prompt_msg2 ; request second number
21: GetInt DX ; DX := second number
22: nwln
23: call sum returns sum in AX
24: PutStr sum_msg display sum
25: PutInt AX
26: nwln
27: done:
28: . EXIT
29:main ENDP
• 1: TITLE Add individual digits of a number ADDIGITS.ASM
• 2: COMMENT I
• 3: Objective: To find the sum of individual digits of
• 4: a given number. Shows character to binary
• 5: conversion of digits.
• 6: Input: Requests a number from keyboard.
• 7: I Output: Prints the sum of the individual digits.
• 8: .MODEL SMALL
• 9: .STACK 100H
• 10: . DATA
• 11: number_prompt DB 'Please type a number <11 digits): ',0
• 12: out_msg DB 'The sum of individual digits is: ',0
• 13: number DB 11 DUP (?)
• 14:
• 15: : . CODE
• 17: main PROC
• 18: . STARTUP
• 19: PutStr number_prompt ; request an input number
• 20: GetStr number,11 ;read input number as a string
• 21: nwln
• 22: mov BX,OFFSET number ; BX := address of number
• 23: sub DX,DX ; DX:= 0 -- DL keeps the sum
• 24: repeat_add:
• 25: mov AL, [BX] ; move the digit to AL
• 26: cmp ; if it is the NULL character
• 27: js done ; sum is done
• 28: and AL,0FH ; mask off the upper 4 bits
• 29: add DL,AL ;add the digit to sum
• 30: inc BX ; increment BX to point to next digit
• 31: jmp repeat_add ; and jump back
• 32: done:
• 33: PutStr out_msg
• 34: Putlnt DX ;write sum
• 35: nwln
• 36: . EXIT
• 37: main ENDP
• 38: END main
.data
array Byte 1,2,3,4,5,6,7,8,9,10 ; test data
multiplier Byte 10 ; test data

BYTE could be WORD or DWORD


• ; This program averages two temperatures
• ; named HI_TEMP and LO_TEMP and puts the
• ; result in the memory location AV_TEMP.
• ; REGISTERS
• ; Uses DS, CS, AX, BL
• ;PORTS
• ; None used
• DATA SEGMENT
• HI_TEMP DB 92h ; Max temp storage
• LO_TEMP DB 52h ; Low temp storage
• AV_TEMP DB ? ; Store average here
• DATA ENDS
• CODE SEGMENT
• ASSUME CS:CODE, DS:DATA
• START: MOV AX, DATA ; Initialize data segment
• MOV DS, AX ; register
• MOV AL, HI_TEMP ; Get first temperature
• ADD AL, LO_TEMP ; Add second lo it
• MOV AH, 00h ; Clear all of AH register
• ADC AH, 00h ; Put carry in LSB of AH
• MOV BL, 02h ; Load divisor in BL register
• DIV BL ; Divide AX by BL. Quotient in AL,
• ; and remainder in AH
• MOV AV_TEMP, AL ; Copy result to memory
• CODE ENDS
• END START
In the above program, the ADD AL,LO_TEMP instruction adds the
specified byte from memory to the contents of the AL register.
It is not possible to directly add the contents of two memory locations,
therefore, HI_TEMP was first moved into a register.
The ADC AH,00h instruction adds the immediate number 00lh plus the
contents of the carry flag to the contents of the AH register. The result
will be left in the AH register.
Since we had cleared AH to all zeros, before the add, what we are really
adding is 00h + 00h + CF.
The result of all this is that the carry flag bit is put in the AH register.

You might also like