Writing a program 

How to write an assembly language program:

These are the steps that should be followed for writing an assembly language program:

  • First, define the problem
  • Write the algorithm
  • Translate into assembly mnemonics
  • Test and Debug the program in case of errors

  • The translation phase consists of the following steps:
  • Define type of data the program will deal with
  • Write appropriate instructions to implement the algorithm
  • Problem:

    In this example you are asked to write a program that calculates the average of two numbers.

    Write the algorithm:

    • add maximum temp and minimum temp to get the sum
    • divide sum by 2 to get average

    Translate into Assembly:

  • what is our data?
  • what are our initialization steps?
  • Here is the listing of the program as it should be given to the computer. It should of course first be assembled and linked.

    Program that calculates the average of 2 numbers
    .model small
    	max_temp DB 92h       
    	min_temp DB 52h
    	avg_temp DB ?
    	mov ax,@data          
    	mov ds,ax
    	mov al, max_temp      
    	add al, min_temp      
    	mov ah, 00h           
    	adc ah, 00h           
    	mov bl, 02h          
    	div bl               
    	mov avg_temp, al     

    Debugging Tips:

    As a general rule, no program is supposed to work as one would expect when it is run for the first time. Therefore, a debugging phase, in which you have to find and correct errors is necessary.
    Here are some tips to help you go through the debugging phase.

    Be sure to work out an algorithm first…not as you go along
    write and test sections of a larger program, rather than waiting until you think you’re done to do any testing
    make sure you coded according to a correct algorithm
    add a set of eyes or watches
    use a debugger such as the codeview that you use in the lab.
    single step through code
    strategically place breakpoints