I need help on figuring out how to do this in assembly language (specifically NA
ID: 640236 • Letter: I
Question
I need help on figuring out how to do this in assembly language (specifically NASM). I am confused how to compare ASCI values from a string (input), and how to store/make an array of ASCI values (by the way it only has to be up to ascii value 127). I don't know really what I am doing when it comes to this array stuff and how the ascii value would be stored or checked against, and how to check the value of each character of a string to the value of an ascii value in the table. I know that probably having two array tables that are indexed the same might work with regard to the ascii frequency table and # of times each have occured in the string, though I don't know if this is the best option. And just to be clear this is to be done in Assembly Language ASM (specifically NASM). And it is suppose to be done as a function. Help would be appreciated.
1. Construct a frequency table for a given string. A frequency table is a table which records counts of each character in a given input. The table should be indexed by ASCII value.
Explanation / Answer
ASCII Representation
In ASCII representation, decimal numbers are stored as string of ASCII characters. For example, the decimal value 1234 is stored as:
Where, 31H is ASCII value for 1, 32H is ASCII value for 2, and so on. There are the following four instructions for processing numbers in ASCII representation:
AAA - ASCII Adjust After Addition
AAS - ASCII Adjust After Subtraction
AAM - ASCII Adjust After Multiplication
AAD - ASCII Adjust Before Division
These instructions do not take any operands and assume the required operand to be in the AL register.
The following example uses the AAS instruction to demonstrate the concept:
When the above code is compiled and executed, it produces the following result:
The CMP Instruction
The CMP instruction compares two operands. It is generally used in conditional execution. This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not. It does not disturb the destination or source operands. It is used along with the conditional jump instruction for decision making.
SYNTAX
CMP compares two numeric data fields. The destination operand could be either in register or in memory. The source operand could be a constant (immediate) data, register or memory.
EXAMPLE:
CMP is often used for comparing whether a counter value has reached the number of times a loop needs to be run. Consider the following typical condition:
Assembly - String Processing
We have already used variable lengths strings in our previous examples. You must have noticed that, the variable lengths strings can have as many characters as required. Generally, we specify the length of the string by either of the two ways:
Explicitly storing string length
Using a sentinel character
We can store the string length explicitly by using the $ location counter symbol that represents the current value of the location counter. In the following example:
$ points to the byte after the last character of the string variable msg. Therefore, $-msg gives the length of the string. We can also write
Alternatively, you can store strings with a trailing sentinel character to delimit a string instead of storing the string length explicitly. The sentinel character should be a special character that does not appear within a string.
For example:
String Instructions
Each string instruction may require a source operand, a destination operand or both. For 32-bit segments, string instructions use ESI and EDI registers to point to the source and destination operands, respectively.
For 16-bit segments, however, the SI and the DI registers are used to point to the source and destination, respectively.
There are five basic instructions for processing strings. They are:
MOVS - This instruction moves 1 Byte, Word or Doubleword of data from memory location to another.
LODS - This instruction loads from memory. If the operand is of one byte, it is loaded into the AL register, if the operand is one word, it is loaded into the AX register and a doubleword is loaded into the EAX register.
STOS - This instruction stores data from register (AL, AX, or EAX) to memory.
CMPS - This instruction compares two data items in memory. Data could be of a byte size, word or doubleword.
SCAS - This instruction compares the contents of a register (AL, AX or EAX) with the contents of an item in memory.
Each of the above instruction has a byte, word and doubleword version and string instructions can be repeated by using a repetition prefix.
These instructions use the ES:DI and DS:SI pair of registers where DI and SI registers contain valid offset addresses that refer to bytes stored in memory. SI is normally associated with DS (data segment) and DI is always associated with ES (extra segment).
The DS:SI (or ESI) and ES:DI (or EDI) registers point to the source and destination operands, respectively. The source operand is assumed to be at DS:SI (or ESI) and the destination operand at ES:DI (or EDI) in memory.
For 16-bit addresses, the SI and DI registers are used, and for 32-bit addresses, the ESI and EDI registers are used.
The following table provides various versions of string instructions and the assumed space of the operands.
SCASD
Assembly - Arrays
We have already discussed that the data definition directives to the assembler are used for allocating storage for variables. The variable could also be initialized with some specific value. The initialized value could be specified in hexadecimal, decimal or binary form.
For example, we can define a word variable months in either of the following way:
The data definition directives can also be used for defining a one-dimensional array. Let us define a one-dimensional array of numbers.
The above definition declares an array of six words each initialized with the numbers 34, 45, 56, 67, 75, 89. This allocates 2x6 = 12 bytes of consecutive memory space. The symbolic address of the first number will be NUMBERS and that of the second number will be NUMBERS + 2 and so on.
Let us take up another example. You can define an array named inventory of size 8, and initialize all the values with zero, as:
Which can be abbreviated as:
The TIMES directive can also be used for multiple initializations to the same value. Using TIMES, the INVENTORY array can be defined as
Example:
The following example demonstrates the above concepts by defining a 3-element array x, which stores three values: 2, 3 and 4. It adds the values in the array and displays the sum 9:
When the above code is compiled and executed, it produces the following result:
Basic Instruction Operands at Byte Operation Word Operation Double word Operation MOVS ES:DI, DS:EI MOVSB MOVSW MOVSD LODS AX, DS:SI LODSB LODSW LODSD STOS ES:DI, AX STOSB STOSW STOSD CMPS DS:SI, ES: DI CMPSB CMPSW CMPSD SCAS ES:DI, AX SCASB SCASWSCASD
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.