Okay so I working on this program where I have to input a string and then display the character distribution in that string.

For example:
if the input is “minecode” the output should be

C – 1

O – 1

D – 1

E – 2

I – 1

M – 1

N – 1


here is what I tried to do but I really don't know how to traverse the loop and check for similar character and then increment the count. Please help me out! The assenbler is MASM 6.15 running on 32 bit machine.

.686
.MODEL flat, stdcall
.STACK
INCLUDE Irvine32.inc
.DATA

msg0 BYTE "Enter a string of characters: ",0
msg1 BYTE "Character Distribution: ",0

MainArray    dword 10000 dup (?)
UniqueChar   dword 10000 dup (?)
CharCount    dword 10000 dup (?)
.CODE
MAIN PROC
    lea edx, msg0
    call WriteString
    call dumpregs        ; 1
    call ReadString
    mov MainArray, eax
    call dumpregs        ; 2
    mov MainArray, ebx
    call dumpregs        ; 3
    call CRLF
    lea edx, msg1
    call WriteString
    call CharDist        ; Calls the procedure
    call dumpregs        ; 5
    exit
MAIN ENDP

CharDist PROC
    mov ecx, lengthof MainArray
    mov esi, OFFSET MainArray

    L1:

; what to do here?? 

Loop L1:


CharDist ENDP

END MAIN

well that's basically quite simple though. Add an array with 26 fields to it where you store the number of occurrences of a character. eg:

countlist  RESB 26
XOR EBX,EBX
XOR EAX,EAX
XOR EDX,EDX
l1:
MOV AL,[ESI+EBX]
SUB EAX,65    ;so that A=0, B=1 etc
MOV DL,[countlist+EAX]
INC DL
MOV [countlist+EAX],DL
INC EBX
LOOP l1

After having executed the code the table has stored the number of all characters in the string.

Remember to clear the array before you call the procedure again.

Thanks but I am sorry that code does not work in my program. Its MASM and .686 model . and 32 bit.

I dont think those are the commands I use.

Anyways why 26 length array? and

what does this line do?

MOV DL,[countlist+EAX]

This line:

MOV dl, [countlist+eax]

Uses the letter as an index into an array of 26 bytes which
represents one of the 26 letters of the english alphabet.
'A'=65 - 65 =0, index 0 into the array, the array's first byte represents
the character A and whenever 'A' is encountered the byte at
index 0 is incremented by one.

JMP get_char_frequency
      countlist RESB 26 ; there should be control transfer 
                        ; (jump) instructions around this
      get_char_frequency:
      XOR EBX,EBX ; initialize registers to zero
      XOR EAX,EAX
      XOR EDX,EDX
      l1:
      MOV AL,[ESI+EBX] ; ESI+EBX points to character in string
      SUB EAX,65 ;so that A=0, B=1 etc ; ASCI value is turned into
                                                   ; index 
      MOV DL,[countlist+EAX] ; use EAX as index into array
                         ; and copy the byte into DL
      INC DL                           ; increment DL by one
      MOV [countlist+EAX],DL ; store back into the array
      INC EBX ; increment index by one to get to next character
                    ; in the string
      LOOP l1
      ... ; do whatever

'hello' would set the bytes within countlist to:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o
0,0,0,0,1,0,0,0,0,0,0,2,0,0,1

Thanks I tried that it gives me invalid operands for line 12 in the above code.

I understood the way it works, but when writing the code, its not doing what its supposed to.

I think the problem is with this

countlist RESB 26

is it an array?

cant I declare it as

countlise    dword 26 dup (?)

Hi vista,

I'm not used to to MASM-syntax as I'm usually working with NASM or the AT&T-syntax. RESB X means "reserve X Bytes". You can use "DB" 26 times as well but makes the code longer.

I guess "dup" stands for duplicate. You have to make sure that you only reserve 26 Bytes of memory. Is there a command like "dbyte"? Or you define just 13 words, as words are two bytes.

LINE 12:
In line 12 I copy the character of the string at position EAX to the register DL. I know it's written different in MASM, but please don't ask me how. Have a look in your handbook.

If you still have questions, feel free to ask =).

Simon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.