Search code examples
assemblymasm

Find the maximum number of consecutive digits in a row


I have some problems with this code. I need to count number of consecutive digits in a row. I use masm assembly language. Here is my code:

 Extrn OutInt:Far       
 data segment
 string db 100 dup ('$')
 string2 db 'Input the string!', 0dh,0ah, '$'
 string3 db 'Count -    $'
 data ends 
 code segment
 assume cs:code,ds:data
 start:
  mov ax, data
  mov ds, ax
  mov ah,9
  lea dx,string2
  int 21h       
  mov ah,0ah
  lea dx,string
  int 21h     
  mov ah,9
  lea dx,string3
  int 21h

  mov si,offset A
  mov BX,0
  mov DX,0
  mov CX,100
  looperunda:
  lodsb
  test AL,10000000b
  je nosigno
  cmp BX,DX
  jnb neatral
  mov BX,DX
  xor DX,DX
  jmp neatral
  nosigno:
  inc DX
  neatral:
  loop looperunda

  mov ax, 16
  Call OutInt   

 code ends
 end start

OutInt:

Title OutInt 
CodeSg  Segment PARA 'Code'
OutInt  Proc FAR
Assume CS:CodeSg
Public OutInt   
aam 
add ax,3030h 
mov dl,ah 
mov dh,al 
mov ah,02 
int 21h 
mov dl,dh 
int 21h

mov ah, 10h
int 16h 
mov ax, 4c00h
int 21h
OutInt endp 
CodeSg ENDS
END OutInt 

The program must work like that : " Input the string!

aa23333c

Count - 4 "

But My code doesn't work. Could anyone help me, please? Where is the mistake?


Solution

  • This can be done with a simple loop: Take every character from string and take his ASCII code as an index for an array. Use the respective array index to count the number of every character and check what character is the most used. Assumptions: No character occurs more than 255 times, only standard-ASCII characters are used.

    .data
    
        myString byte "aa23333c", 0
        countArray byte 255 DUP(0)
    
    .code
        main PROC
    
        mov bx, 0    ;// Exchange bx with eax in this line when using a 32 bit system
        mov si, -1
        STRING_LOOP:
            inc si
            movzx ax, BYTE PTR [myString+si]
            add BYTE PTR [countArray+ax], 1
            cmp BYTE PTR [countArray+ax], bl
            jb NEXT_CHAR
            mov bl, BYTE PTR [countArray+ax]
            mov bh, BYTE PTR [myString+si]
            NEXT_CHAR:
            and ax, 0FFh
        jnz STRING_LOOP
        ;// character code is now in bh
        ;// numer of iterations is now in bl
    
    main ENDP
    
    END main