An Assembly program which adds the sales tax in the Price list of items and replace the Price list with the Calculated value

By | March 21, 2014

Now we will write another Assembly program to calculate the average of three given numbers stored in memory.

Let’s identify variables needed for this program.
First variables will be the one which will hold the values present in the Price list and it will be array PRICE. Other variables will be holding the Sale’s Tax in Percentage form. Other variables will be holding Length of the Array and it will be LENand it will be TAX So in all Three variables.
The identified variables are PRICE, LEN and TAX.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.

Next Line – PRICE DB 36,55,27,42,38,41,29,39
     LEN DW $-PRICE
     TAX DB 12

 PRICE DB 36,55,27,42,38,41,29,39 this line is a declaration of 8-bit Numbers Array initialized with 36H,55H,27H,42H,38H,41H,29H,39H the numbers are seperated by Comma (,). LEN DW $-PRICE is used to Save the Length of the Array which will be generated by $-Name of the array i.e. $-PRICETAX DB 12 is used to Save the Sales’s Tax Percentage which value 12 decimal value (12%) which will be Calculated to each element of a Array PRICE. Detailed explanation is given below.

Next Line – DATA ENDS

DATA ENDS is the End point of the Data Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Data Segment.

Now, Selection of data type is DB data type the numbers which we are adding will be integers so DB is sufficient.

DATA SEGMENT
     PRICE DB 36,55,27,42,38,41,29,39
     LEN DW $-PRICE
     TAX DB 12
DATA ENDS

 In Assembly programming, the variable are all defined by bytes only.

DB – Define Byte  (Size – 1 Byte)

DW – Define Word  (Size – 2 Byte)

DD – Define Double word  (Size -  4 Bytes)

DQ – Define Quad word  (Size – 8 Bytes)

DT – Define Ten Bytes  (Size – 10 Bytes)

NUMBER SYSTEM in Assembly Programming is Decimal, Octal, Hexadecimal, Binary.

In the Program, We are entering the values for the variables and Do arithmetical Operations like Addition, Subtraction, Multiplication and Division So the Computer should understand which kind of Number is entered. Hence there is a different letters for different Number Systems. O or o stands for Octal, H or h stands for Hexadecimal, B or b stands for Binary, D or d stands for Decimal. By default type of numbering system is Decimal. If you do not specify any letter then the number is understood to be Decimal (By default).

DATA SEGMENT
     PRICE DB 36,55,27,42,38,41,29,39
     LEN DW $-PRICE
     TAX DB 12
DATA ENDS
CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
     
      LEA BX,PRICE
     
       MOV CX,LEN
LOOP1:      
       MOV AH,0
       MOV AL,[BX]
                           
       MOV DL,TAX
       MUL DL
      
       MOV DL,100
       DIV DL
                   
       MOV AH,[BX]
       ADD AH,AL
      
       MOV [BX],AH         
       INC BX
             
       LOOP LOOP1   
     
      MOV AH,4CH
      INT 21H     
ENDS
END START

Explanation : 

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

Next Line – CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line – LEA BX,PRICE

LEA BX,PRICE in this LEA stands for LOAD EFFECTIVE ADDRESS and it loads the effective address of second element into the first element.  This same code can be interchangably written as MOV DX, OFFSET PRICE where OFFSET  means effective address and MOV means move  second element into the first element. Here Base Address of variable PRICE is loaded in DX register.

Next Line – MOV CX,LEN

 MOV CX,LEN is used to move or assign value 8 (Length of Array) to  CX. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – LOOP1:

LOOP1: is a LABEL and all the words ending in colon (:).

Next Line – MOV AH,0

MOV AH,0 is used to clear the unwanted value (garbage value) in AH register is removed by assigning ZERO to it.

Next Line –  MOV AL,[BX]

MOV AL,[BX]  means move value in Address of BX register to AL register. [] is Refered as Value present in the Address of the Register in it.

Next Line – MOV DL,TAX
       MUL DL

The above line code is used to Multiply Element of Array by TAX  i.e.  value present in AX register

MUL TAX cannot be used hence We first move TAX to DL register i.e. MOV DL,TAX. After that MUL DL in this line DL register will be Multiplied with AX register (BY DEFAULT) Now, This will give (PRICE * 12).

Next Line – MOV DL,100
    DIV DL

The above line code is used to Divide AX register by 100 i.e. (PRICE * 12) value present in AX register

DIV 100 cannot be used hence We first move 100 to BX register i.e. MOV DL,100. After that DIV DL in this line value in BX register will Divide  AX:DX register (BY DEFAULT) in which the resultant value of (PRICE * 12) is present. Now, This will give (PRICE * 12)/100.

Next Line – MOV AH,[BX]
       ADD AH,AL

MOV AH,[BX]  means move value in Address of BX register to AL register. [] is Refered as Value present in the Address of the Register in it. So that 12% which is in AL register can be added with original Price moved in AH register. ADD AL,TAX means move value in AL register from variable TAX. DAA means Decimal Adjust after Addition. e.g. 36H is present in AL register, If added with 0AH will give 40H. After DAA it will Convert to Decimal equivalent to 46H.

Next Line –  MOV [BX],AL

MOV [BX],AL  means move value in AL register to Address of BX register. [] is Refered as Value present in the Address of the Register in it.

 Next Line –  INC BX

INC BX will increment the Address value present in BX register. Here we are using BX register as a counter and counting the numbers of Array elements to Cover all the Eight Prices of Items.

Next Line – LOOP LOOP1

This end of loop. In assembly programming language we have a LOOP instruction. This works with two other helpers which are Label and Counter. The Loop start with LABEL and ends with LOOP instruction with the same LABEL name with it. the execution of the Loop depends on the value in CX register ( CX is also Called COUNTER).

Next Line – MOV AH,4CH
INT 21H

The above two line code is used to exit to dos or exit to operating system. Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 4ch, That means Return to Operating System or DOS which is the End of the program.

Next Line – CODE ENDS

CODE ENDS is the End point of the Code Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Code Segment.

Last Line – END START

END START is the end of the label used to show the ending point of the code which is written in the Code Segment.

Note :- In this Assembly Language Programming, We have Com format and EXE format. We are Learning in EXE format only which simple then COM format to understand and Write. We can write the program in lower or upper case, But i prepare Upper Case.

Screen Shots :-

 Asm_program_Calc_SalesTaxToList

Before Execution :-

Asm_program_Calc_SalesTaxToList_Output1

After Execution :-

Asm_program_Calc_SalesTaxToList_Output2

Note :- To see the variable and its value you have to click vars button in the emulator

Leave a Reply