Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Everrything has to be in assembly. Please read direction before posting the solu

ID: 3572525 • Letter: E

Question

Everrything has to be in assembly. Please read direction before posting the solutions.

Write a program all written in assembly that inputs an entire line of text, up to 132 characters, into a buffer. Then have your program determine how many time each letter of the alphabet occurs You are to count all letters a-z, A-Z, and 0-9 only Your screen output is to look similar to a - 6 times b - 3 times c - 5 times etc, Only display the letters you are counting. You will declare the a input buffer called CharInput (i.e. array) and write the code to count the number of a's that occur, b's, etc and then display them. You will have another buffer CharCount that will hold the char accounts. You could have 3 program loops that address each of the 3 groups of alphanumeric characters (a-z, A-Z, and 0-9)

Explanation / Answer

.stack 100h

.data

msj1      db 'Enter a line of text (132 max) : $'

text      db 133 ;MAX LENGTH 132, PLUS 1 FOR ENDING CHR(13).

          db ?    ;LENGTH ENTERED BY USER.

          db 133 dup(?) ;THE STRING.

digits    dw 10 dup(0) ;ARRAY OF 10 COUNTERS FOR DIGITS.

uppercase dw 26 dup(0) ;ARRAY OF 26 COUNTERS FOR UPPERCASE LETTERS.

lowercase dw 26 dup(0) ;ARRAY OF 26 COUNTERS FOR LOWERCASE LETTERS.

str       db 6 dup('$') ;TO STORE NUMBER CONVERTED TO STRING.

char      db ' =$'

space     db ' $'

.code         

;INITIALIZE DATA SEGMENT.

mov ax,@data

mov ds,ax

;DISPLAY MESSAGE TO CAPTURE LINE OF TEXT.

mov ah, 9

mov dx, offset msj1

int 21h

;CAPTURE LINE OF TEXT.

mov ah, 0AH

mov dx, offset text

int 21h

;COUNT CHARACTERS OF THE LINE OF TEXT.

mov si, offset text + 2 ;SI POINTS TO BEGINNING OF STRING. THE FIRST

                            ;2 BYTES ARE FOR CONTROL (MAX LENGTH, LENGTH).

repeat:

;CHECK IF CURRENT CHARACTER IS DIGIT, UPPERCASE OR LOWERCASE.

mov bl, [ si ] ;GET CURRENT CHARACTER.

call check_digit

call check_uppercase

call check_lowercase

;NEXT CHARACTER.

inc si

mov bl, [ si ]

cmp bl, 13

jne repeat ;IF NEXT CHARACTER IS NOT 13, REPEAT.

;DISPLAY COUNTERS.

call clear_screen

call display_digit_counters   

call display_uppercase_counters   

call display_lowercase_counters   

;WAIT FOR A KEY.

mov ah,7

int 21h

;FINISH PROGRAM.

mov ax,4c00h

int 21h                  

proc display_digit_counters

mov di, offset digits ;DI POINTS TO ARRAY.

mov bp, 10 ;ARRAY LENGTH.

display_digits:

;DISPLAY SPACE.

mov ah, 9

mov dx, offset space

int 21h

;CONVERT CURRENT COUNTER IN STRING.

call dollars

mov ax, [ di ]

call number2string ;STRING RETURNS IN VARIABLE "STR".

;CONVERT CURRENT COUNTER IN CORRESPONDING DIGIT. FOR

;EXAMPLE, IF CURRENT COUNTER IS 3, CONVERT 3 IN '3'.

mov ax, di            ;GET CURRENT COUNTER OFFSET.

sub ax, offset digits ;CONVERT TO 0..9.

shr ax, 1             ;DIVIDE OFFSET BECAUSE COUNTERS ARE DW.

add al, 48            ;CONVERT TO '0'..'9'.

mov char, al          ;STORE DIGIT IN STRING TO DISPLAY.

;DISPLAY DIGIT FOR CURRENT COUNTER BELONGS TO.

mov ah, 9

mov dx, offset char

int 21h

;DISPLAY CURRENT COUNTER.        

mov ah, 9

mov dx, offset str

int 21h

;NEXT COUNTER TO DISPLAY.

add di, 2 ;EVERY COUNTER IS DW (TWO BYTES).

dec bp

jnz display_digits ;IF ( BP > 0 ) JUMP.

ret

endp   

proc display_uppercase_counters

mov di, offset uppercase ;DI POINTS TO ARRAY.

mov bp, 26 ;ARRAY LENGTH.

display_uppercase:

;DISPLAY SPACE.

mov ah, 9

mov dx, offset space

int 21h

;CONVERT CURRENT COUNTER IN STRING.

call dollars

mov ax, [ di ]

call number2string ;STRING RETURNS IN VARIABLE "STR".

;CONVERT CURRENT COUNTER IN CORRESPONDING LETTER. FOR

;EXAMPLE, IF CURRENT COUNTER IS 3, CONVERT 3 IN 'D'.

mov ax, di            ;GET CURRENT COUNTER OFFSET.

sub ax, offset uppercase ;CONVERT TO 0..25.

shr ax, 1             ;DIVIDE OFFSET BECAUSE COUNTERS ARE DW.

add al, 65            ;CONVERT TO 'A'..'Z'.

mov char, al          ;STORE LETTER IN STRING TO DISPLAY.

;DISPLAY LETTER FOR CURRENT COUNTER BELONGS TO.

mov ah, 9

mov dx, offset char

int 21h

;DISPLAY CURRENT COUNTER.        

mov ah, 9

mov dx, offset str

int 21h

;NEXT COUNTER TO DISPLAY.

add di, 2 ;EVERY COUNTER IS DW (TWO BYTES).

dec bp

jnz display_uppercase ;IF ( BP > 0 ) JUMP.

ret

endp   

proc display_lowercase_counters

mov di, offset lowercase ;DI POINTS TO ARRAY.

mov bp, 26 ;ARRAY LENGTH.

display_lowercase:

;DISPLAY SPACE.

mov ah, 9

mov dx, offset space

int 21h

;CONVERT CURRENT COUNTER IN STRING.

call dollars

mov ax, [ di ]

call number2string ;STRING RETURNS IN VARIABLE "STR".

;CONVERT CURRENT COUNTER IN CORRESPONDING LETTER. FOR

;EXAMPLE, IF CURRENT COUNTER IS 3, CONVERT 3 IN 'd'.

mov ax, di            ;GET CURRENT COUNTER OFFSET.

sub ax, offset lowercase ;CONVERT TO 0..25.

shr ax, 1             ;DIVIDE OFFSET BECAUSE COUNTERS ARE DW.

add al, 97            ;CONVERT TO 'a'..'z'.

mov char, al          ;STORE LETTER IN STRING TO DISPLAY.

;DISPLAY LETTER FOR CURRENT COUNTER BELONGS TO.

mov ah, 9

mov dx, offset char

int 21h

;DISPLAY CURRENT COUNTER.        

mov ah, 9

mov dx, offset str

int 21h

;NEXT COUNTER TO DISPLAY.

add di, 2 ;EVERY COUNTER IS DW (TWO BYTES).

dec bp

jnz display_lowercase ;IF ( BP > 0 ) JUMP.

ret

endp   

proc check_digit       

cmp bl, '0'

jb   not_a_digit ;IF AL < '0' FINISH.

cmp bl, '9'

ja   not_a_digit ;IF AL > '9' FINISH.

;IF NO JUMP, AL IS A DIGIT. INCREASE COUNTER USING

;THE DIGIT AS OFFSET INSIDE THE ARRAY OF COUNTERS,

;FOR EXAMPLE, THE COUNTER FOR DIGIT '3' IS THE THIRD

;POSITION IN THE ARRAY.

sub bl, 48 ;CONVERT DIGIT IN NUMBER ('0'..'9'->0..9).

mov bh, 0 ;CLEAR BH TO USE BX.                       

shl bx, 1 ;MULTIPLY BY 2 BECASE EVERY COUNTER IS DW (2 BYTES).

add bx, offset digits ;BX POINTS TO THE APPROPIATE COUNTER.

inc [ bx ] ;INCREASE COUNTER OF DIGIT.

not_a_digit:

ret

endp

proc check_uppercase

cmp bl, 'A'

jb   not_uppercase ;IF AL < 'A' FINISH.

cmp bl, 'Z'

ja   not_uppercase ;IF AL > 'Z' FINISH.

;IF NO JUMP, AL IS AN UPPERCASE LETTER. INCREASE COUNTER

;USING THE LETTER AS OFFSET INSIDE THE ARRAY OF COUNTERS,

;FOR EXAMPLE, THE COUNTER FOR LETTER 'C' IS THE THIRD

;POSITION IN THE ARRAY.

sub bl, 65 ;CONVERT DIGIT IN NUMBER ('A'..'Z'->0..25).

mov bh, 0 ;CLEAR BH TO USE BX.

shl bx, 1 ;MULTIPLY BY 2 BECASE EVERY COUNTER IS DW (2 BYTES).

add bx, offset uppercase ;BX POINTS TO THE APPROPIATE COUNTER.

inc [ bx ] ;INCREASE COUNTER OF UPPERCASE LETTER.

not_uppercase:

ret

endp

proc check_lowercase

cmp bl, 'a'

jb   not_lowercase ;IF AL < 'a' FINISH.

cmp bl, 'z'

ja   not_lowercase ;IF AL > 'z' FINISH.

;IF NO JUMP, AL IS A LOWERCASE LETTER. INCREASE COUNTER

;USING THE LETTER AS OFFSET INSIDE THE ARRAY OF COUNTERS,

;FOR EXAMPLE, THE COUNTER FOR LETTER 'c' IS THE THIRD

;POSITION IN THE ARRAY.

sub bl, 97 ;CONVERT LETTER IN NUMBER ('a'..'z'->0..25).

mov bh, 0 ;CLEAR BH TO USE BX.

shl bx, 1 ;MULTIPLY BY 2 BECASE EVERY COUNTER IS DW (2 BYTES).

add bx, offset lowercase ;BX POINTS TO THE APPROPIATE COUNTER.

inc [ bx ] ;INCREASE COUNTER OF LOWERCASE LETTER.

not_lowercase:

ret

endp

proc number2string

mov bx, 10 ;DIGITS ARE EXTRACTED DIVIDING BY 10.

mov cx, 0 ;COUNTER FOR EXTRACTED DIGITS.

cycle1:      

mov dx, 0 ;NECESSARY TO DIVIDE BY BX.

div bx ;DX:AX / 10 = AX:QUOTIENT DX:REMAINDER.

push dx ;PRESERVE DIGIT EXTRACTED FOR LATER.

inc cx ;INCREASE COUNTER FOR EVERY DIGIT EXTRACTED.

cmp ax, 0 ;IF NUMBER IS

jne cycle1 ;NOT ZERO, LOOP.

;NOW RETRIEVE PUSHED DIGITS.

mov si, offset str

cycle2:

pop dx       

add dl, 48 ;CONVERT DIGIT TO CHARACTER.

mov [ si ], dl

inc si

loop cycle2

ret

endp

proc dollars                

mov si, offset str

mov cx, 6

six_dollars:     

mov al, '$'

mov [ si ], al

inc si

loop six_dollars

ret

endp

proc clear_screen

mov ah,0

mov al,3

int 10H

ret

endp

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote