Tutorial Details
Completion Time: 40 min
Difficulty: Intermediate
Technology: 8086
Language: Assembly
This entry is part 2 of Learn to program in 8086
[hr]
This tutorial teaches you ho to manage character’s array in 8086 programming. We will first learn character’s array declaration and counting occurrences of characters in a character’s array and finally character’s array transformation with a caesar cipher.
At the end of the article , you can download the source code.
1. Declare a character’s array
One way to declare a character’s array is to specify the name and the dimension of your character’s array, the size of every element and the special system word DUP. Exactly like your were declaring a simple array .
Size of element can be DB( for byte) or DW ( for word that means 2 bytes).
Example:
MY_STRING DB DIM DUP(?)
MY_STRING is an array of dimension DIM (a constant) without initial values.
2. Count the occurences of each character of a character’s array
Let’s assume we have a character’s array MY_STRING, we will count the number of times each character appears (desired characters are only a..z, A…Z), printing, for each row, the character that appears a number of times equal the maximum times.
First step
we need to know if a character is in a..z, A…Z.
we will use the ascii table to verify if the character is valid.
We can see that we are interest in characters with ascii code between 65 and 90 or between 97 and 122
Second step
we will declare another array of 26 number with initial values zero (OCCURENCE). for every character in our variable MY_STRING, we will increment the correspondant value in the OCCURENCE array.
How we find the right position of a character in the OCCURENCE array? Simple, we subtract to ascci code of the character the ascci code of the first letter a/A.
Third step
We need the maximum number of the OCCURENCE array. We use an algorithm similar to the one seen on the previous article
Fourth step
The code:
;Fill OCCURENCE MOV CX,DIMF MOV DI,0 XOR AX,AX cycle:MOV AL,MY_STRING[DI] CMP AL,41h ; A JL end_continue; before A = invalid CMP AL,7Ah ; z JG end_continue; after z = invalid CMP AL,5Ah ; Z JLE upper_case ;after A and before Z = valid and upper case CMP AL,61h ; a JL end_continue;after A, after Z and before a = invalid ; after a and before z = valid MOV BL,61h ; 'a' continue: SUB AL,BL ;char - 'a'/'A' MOV BL,AL MOV BH,0 INC OCCURENCE[BX] end_continue: INC DI DEC CX CMP CX,0 JNZ cycle JMP end_count upper_case: MOV BL,41h ; 'A' JMP continue end_count: ;Find max value of OCCURENCE ;big = element[0] ;for each element in array, starting from 1 (not 0): ; if (element > big): big = element ; ; MOV DI, 0 MOV AL,OCCURENCE[DI] MOV INDEX,DI MOV CX, DIM2-1 ; DIM2 == 26 MOV DI, 1 find:CMP AL,OCCURENCE[DI] JAE PASS MOV AL,OCCURENCE[DI] MOV INDEX,DI;INDEX is a variable to track the max value position PASS:INC DI DEC CX CMP CX,0 JNZ find MOV AX,INDEX ADD AX,61h ; index + 'a' MOV MAX_CHAR,AL
MAX_CHAR is the charater with max occurences in MY_STRING.
3. Transform a character’s array using a Caesar cipher
What is a caesar cipher ? A Caesar cipher transforms letter a in a+k, given this succession of characters a..zA..Za…z…
Here we will simply take every character and add k to his ascci code with one exeption (if the new ascii code is after the ‘z’/’Z’ ascii code).
from ‘Z’ to ‘a’, we have 6 characters. we will add 6 to the ascci code of our overflowing character.
from ‘A’ to ‘z’, we have 58 characters. we will substract 58 to the ascci code of our overflowing character.
; Let's transform MY_STRING MOV CX,DIM MOV DI,0 XOR AX,AX caesar: MOV AL,MY_STRING[DI] CMP AL,41h ; A JL end_test_caesar CMP AL,7Ah ; z JG end_test_caesar CMP AL,5Ah ; Z JLE upper_case CMP AL,61h ; a JL end_test_caesar MOV BL,7Ah test_caesar: ADD AL,K; letter + k CMP AL,7Ah JG modulo2 MOV MY_STRING[DI],AL end_test_caesar: INC DI DEC CX CMP CX,0 JNZ caesar JMP end_caesar upper_case: ADD AL,K; letter + k CMP AL,5Ah JG modulo1 MOV MY_STRING[DI],AL JMP end_test_caesar modulo1: ADD AL,6 ; after z -> A MOV MY_STRING[DI],AL JMP end_test_caesar: modulo2: MOV BL,58 SUB AL,BL ; after Z -> a MOV MY_STRING[DI],AL JMP end_test_caesar: end_caesar:
4. Source code
5. Links and Literature
ascii article on wikipedia
Caesar cipher article on wikipedia
6. Thank you
Support my challenge, support my blog. Please Share this article if you found it useful. Thank you.