i need one program in assembly 8086
input : one sentence (string) output : sort word by word ascending or Descending as user wish
please help
i need one program in assembly 8086
input : one sentence (string) output : sort word by word ascending or Descending as user wish
please help
Copyright © 2021 Jogjafile Inc.
Since you are requesting help for homework, it's better to focus on algorithm rather than code.
I assume you are programming in DOS environment, if this is not the case, just translate the concepts to your OS.
The first thing you need to do is asking the user for input. In DOS there are several way to do this, depending on your need you have to choose the one that fits the best. I think a buffered input is the best option here, use
INT 21/AH=0ah
. You can use an online version of the Ralph Brown Interrupt List for reference.One you have a buffer with the input string you need to scan it and recognize the words. Words are separated by specific characters, let's assume only a space (ASCII code 20h) character is used as word separator. You cannot assume that between two consecutive words only a single space appears.
Recognizing the start and the end of words is easy but how do you save a word once you have its start and end?
Words are strings and the problem with strings is that they have variable length, so you cannot copy them in a fixed size buffer and since you will have to swap them in order to sort the words you need to use an indirect layer.
Instead of copying the words from the input string, terminate each word with the DOS string terminator
$
(ASCII code 24h) and save the pointers to their beginnings. Here is an ugly picture of the conceptNow you are reduce to the problem of sorting an array of pointers to strings, an easy task.
You first need a string comparator, something like
strcmp
, a function that compare string A and B and return -1 if A < B, 0 if A = B and 1 if A > B. There are many orders on strings, the Lexicographical order is what is intended by the problem. You can find tutorials on this subject all over the internet.After the comparator you need a sort algorithm, Bubble sort is very inefficient but very easy to implement. The trick here is that you compare the strings but swap the pointers!
Finally you just have to print the string in the order you find them in the array.
I have made a simple DOS COM program that solve you problem. I warn you that you should use it only as a sample, it uses a lot of instructions not present in the original 8086, and I create the input buffer and the array on the stack. Anyway here is its output (in DOSBox) and below the source code (for NASM)