Add Single Digit Numbers With Filtering

This problem is easily solved if we are to use High level programming language, but for assembly language, we need to remember that each entry from the keyboard are accepted as hexadecimal equivalent of characters, even if the entry is a digit, as long as it is from the keyboard. To manipulate these data, we need to be able to work with the registers and the memory allocation since that is the only thing we can work with during processing when.

So here's how we did it:
Since we are just entering a single digit number, we just use the 

MOV ah, 01h 
INT 21h

to enter a character which will then be stored inside AL
for the first number, we transfered it to CL to not loose the data since we are to use AL again for the second input. We then decrement the inputed characters by 30H to get its equivalent numerical value in hexadecimal.

To determine if the input is valid or not, right after we enter the data, we test it first if the value is between 30h to 39h, since the characters '0' to '9' is only within that range, we use the compare then jump algorithm. JL for Jump if Lesser. JG for Jump if Greater. JE for Jump if Equal.

CMP AL, 30H
JL

The code above compares the AL to 30H, and if it is lesser than the value(which is lesser than the valid range for numerical characters), we let it jump to the label where we want it to go to.

CMP AL, 39H
JG

The code above compares the AL to 39H, and if it is greater than the value(which is higher than the valid range for numerical characters), we let it jump to the label where we want it to go to.

At the second input, we leave the second number to AL since later, we are to use AAA(ASCII Adjust for Addition) in case our result of the addition will exceed 09h. During the AAA, if it exceeds to 09h, the AL will be added by 06H to get the appropriate value then zero out the most significant nibble, then add AH by 1, that is also why we zeroed out first the AH in order for our highest significant digit to start at 0.

After adjustment, we add the two values with 30H to bring it back to its ASCII equivalent(ADD AX, 3030H). We then PUSH the data (which is stored inside AX momentarily) to the stack to be stored in the MM to prevent data loss(PUSH AX). 

Then later, when we are ready to print the result, we just simply POP out the result (POP CX) then store it to CX and one by one display the characters starting from the highest significant number.

There is another filtering during the displaying of the result, that is if the most significant number of the result is equal to zero, which means that the result of the addition did not exceed 09h.

CMP AH, 30H
JE

The code above is to let the program skip a printing the the Highest Significant Number if the it is equal to 30H or simply '0' to the specified label for the printing of the Lowest Significant Number.


For the output: I provided 3 situations
1st situation is when we add values where the result is greater than 9.
2nd situation is when we add values within 0 - 9.
3rd situation is when there is an invalid input.

0 comments:

Post a Comment