Home > Software design >  Assembly language 8086 - How to set a size for the input & how to calculate it?
Assembly language 8086 - How to set a size for the input & how to calculate it?

Time:09-18

I want to know how to set the length for the input and also how to get the size of the string that is entered by the user.

For example, they might enter 9 or 99 to represent the number of tickets they want to buy, but I do not want to limit them to enter 2 digits every time like 09 for singular, I want that they can enter 9$ or 99$. After that based on the length of the input, the number will be treated as different values, if the input is only 1 digit then the first digit will be represented as one, if the input is a 2 digit input then the first digit will represent tens.

After that I will multiply using the formulae of:

Total = 1.5 * number of stops traveled * number of ticket * 105%

How do I perform the calculation?

CodePudding user response:

I want to know how to set the length for the input and also how to get the size of the string that is entered by the user.

The easiest would be to use the DOS.BufferedInput function 0Ah. You can read about it in How buffered input works.

set the length

You seem to expect user input that has 1 or 2 digits. Then a buffer with 3 bytes is fine; room for at most 2 characters and a terminating carriage return.

INBUF  db 3, 0, 3 dup (0)

You use it like:

lea dx, INBUF         
mov ah, 0Ah  
int 21h

get the size

This is how you read the length of the inputted text:

mov cl, [INBUF   1]
mov ch, 0                ; -> CX is either 1 or 2

Assuming the user inputs 5 enter, then from the code above the CX register would hold 1. You then convert the character "5" into the value 5 by subtracting 48, like in:

mov al, [INBUF   2]
sub al, 48

Total = 1.5 * number of stops traveled * number of ticket * 105%

How do I perform the calculation?

That's a whole different question for which you should show us your best effort. I think that with the help of today's answer, you should be able to start working on that...

One tip though: don't let the decimal point fool you, you can calculate this entirely using integer instructions.

CodePudding user response:

From the tag x86-16 I assume that you want your program run in DOS.

You can use DOS function READ FROM FILE OR DEVICE. Set the file handle to BX=0 ; Standard input handle, specifiy the buffer size in register CX and invoke the function.
Number of characters, which the user has actually entered, is then returned in register AX.

  • Related