flat assembler
Message board for the users of flat assembler.

Index > Main > Count the number of a particular character in a string

Goto page 1, 2, 3, 4  Next
Author
Thread Post new topic Reply to topic
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 09:08
I need some help with an assembler program. What I have to do is to read a string of max 20 characters and one another character. I don't understand why I can't read more than 12 characters.


Last edited by MariaSM on 04 May 2014, 17:53; edited 1 time in total
Post 03 May 2014, 09:08
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 03 May 2014, 10:18
Which OS are you targeting? It looks like DOS code but maybe you are running it on a different OS or emulator?

Your code appears to be MASM or TASM style. Is it just that you need help to convert to fasm format? Or do you also need help in debugging?


Last edited by revolution on 03 May 2014, 10:52; edited 1 time in total
Post 03 May 2014, 10:18
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 10:31
It is TASM, assembler i8086.
I only need some help in debugging. I want to know if the comparison and count of character is good. and how to make it display the number of appearances..


Last edited by MariaSM on 04 May 2014, 17:53; edited 1 time in total
Post 03 May 2014, 10:31
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 03 May 2014, 10:45
I am having trouble parsing this instruction:
Code:
LEA DX,CL    
Are you sure that compiles? If so what does it compile to?
Post 03 May 2014, 10:45
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 10:48
no.it doesn't compile..I have tried in other ways, but really I don't know how to replace that...because even if it compiles.id doesn't display anything..
Post 03 May 2014, 10:48
View user's profile Send private message Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 03 May 2014, 11:01
Code:
 E5: 
MOV AH,09H 
LEA DX,COUNT 
LEA DX,CL 
INT 21H     


You're definitely doing it wrong.
you first need to convert number in CL to ASCII, then append it to your string COUNT, and then use Fn 09h of Int 21h to display the whole string

convert 8-bit number to ascii:
Code:
COUNT DB "Number of appearances of S2 in S1: "
NUMBER DB 2 DUP(30h)
DB "$"

lea si, number
mov al, cl
shr al, 4
or al, 30h
cmp al, 3Ah
jb LB1
add al, 7
LB1:
mov [si], al
inc si
and cl, 0Fh
or cl, 30h
cmp cl, 3Ah
jb LB2
add cl, 7
LB2:
mov [si], cl

MOV AH, 09h
LEA DX, COUNT
INT 21h    

something like that. Above, you split 8-bit number into 2 nibbles (4-bit). first you convert upper nibble (in AL) to ascii and store it in your buffer, then lower nibble (in CL)

EDIT: forgot to mention: you'll get number in hex this way.
Post 03 May 2014, 11:01
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 11:10
I replaced that part but it doesn't display anything
how about counting and comparing? is this ok?
Post 03 May 2014, 11:10
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 12:00
but al and cl doesn't need to have 8bits each other?
Post 03 May 2014, 12:00
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1635
Location: Toronto, Canada
AsmGuru62 03 May 2014, 12:08
The string entering code misses the max # of characters field:
http://spike.scu.edu.au/~barry/interrupts.html#ah0a
First you need two bytes BEFORE the buffer and they must be set according to the link above.
So, you need 20 characters in text and 1 byte for Carriage Return plus 2 bytes for the header.
That makes it something like this:
Code:
bufMaxLen DB 20   ; Max # of characters
bufEntered DB 0     ; This is how many characters user entered
bufText DB 0 DUP(21)  ; 20 characters + CR byte

... to enter the string ...
lea dx, bufMaxLen
mov ah, 0Ah
int 21h

... to count characters ...
lea si, bufText
.. and go on ...
    

To output the value in CL you can just use a code like that:
(knowing that the value can be in range 0 to 20)
Code:
if (value in CL <= 9)
{
        CL + '0'
        print CL
}
else if (CL = 20)
{
        print '20'
}
else  ; CL between 10 and 19
{
        print '1'
        CL - 10
        CL + '0'
        print CL
}
    

But of course, it is better to learn how to convert any number to a string.
If you search the forum -- there are few examples of this.
Post 03 May 2014, 12:08
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 03 May 2014, 12:26
Just use DIV (or AAM) to split the number into decimal sizes. Don't be afraid of the DIV just because it is a scary long latency instruction. When you are processing user input it makes not a jot of difference to the perceived experience. Nobody can sense the extra few nanoseconds it took to complete.
Post 03 May 2014, 12:26
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 13:00
so..it is or not okay the comp and counting?
Post 03 May 2014, 13:00
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1635
Location: Toronto, Canada
AsmGuru62 03 May 2014, 13:19
What exactly is "comp and counting"?
Post 03 May 2014, 13:19
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 13:57
the part of code when I compare and count the number of appearences of the character in the string


Last edited by MariaSM on 06 May 2014, 05:21; edited 1 time in total
Post 03 May 2014, 13:57
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1635
Location: Toronto, Canada
AsmGuru62 03 May 2014, 14:45
I would suggest this:
Code:
lea si, <text>
mov cx, <text length>
mov al, <character to look for>
xor ah, ah   ; AH=0 to count them

test_character:
cmp [si], al
jne next_char
;
; Count it
;
inc ah

next_char:
inc si
loop test_character
;
; At this point the # of counted characters is in AH
;
    
Post 03 May 2014, 14:45
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 15:51
AsmGuru62 wrote:
I would suggest this:
Code:
lea si, <text>
mov cx, <text length>
mov al, <character to look for>
xor ah, ah   ; AH=0 to count them

test_character:
cmp [si], al
jne next_char
;
; Count it
;
inc ah

next_char:
inc si
loop test_character
;
; At this point the # of counted characters is in AH
;
    


thanks.
and what should I do to display what it is in AH?
Post 03 May 2014, 15:51
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 16:01
And one another problem. why I can't read more than 12 charcater ?
Post 03 May 2014, 16:01
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20355
Location: In your JS exploiting you and your system
revolution 03 May 2014, 16:08
MariaSM wrote:
And one another problem. why I can't read more than 12 charcater ?
This was answered above. But basically you have told the OS that you only want 12 characters because your buffer is initialised to have the first byte as 0dh (13 decimal), so once you include the zero terminator character the length maxes out at twelve characters.
Post 03 May 2014, 16:08
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 16:18
revolution wrote:
MariaSM wrote:
And one another problem. why I can't read more than 12 charcater ?
This was answered above. But basically you have told the OS that you only want 12 characters because your buffer is initialised to have the first byte as 0dh (13 decimal), so once you include the zero terminator character the length maxes out at twelve characters.


and what should I do for letting me read 20 characters?
sorry for so many questions but i haven't worked in assembler until now.and I don't know many things..
Post 03 May 2014, 16:18
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie 03 May 2014, 16:34
MariaSM

Seems like it may take you forever to understand revolution and ASMGuru here. I modified this code from my library and I intentionally leave one part buggy for you to figure out. It deals only with lower case characters. Learn from it and read carefully of what Guru and revolution are saying. Take your time.

Code:
org 100h
        xor si,si
;------------------------
get:    call get$
        cmp al,0dh
            je ok
        mov [chr+si],al
        sub al,61h
        movzx di,al
        add [chr2+di],1
        inc si
        cmp si,20
            je ok
        jmp get
ok:     call nline
        xor di,di
;-------------------------
done:   cmp [chr2+di],'a'
            ja change
        inc di
good:   cmp di,24
            je exit
        jmp done
change: xor ax,ax
        mov al,[chr2+di]
        sub al,61h
        mov dl,[chr2+di]
        call prtc
        mov dl,'='
        call prtc
        call prtnum
        call nline
        inc di
        cmp [chr2+di],'a'
            ja change
        jmp good
;--------------------------
get$:   mov ah,1
        int 21h
        ret
nline:  mov dl,0dh
        call prtc
        mov dl,0ah
        call prtc
        ret
prtc:   push ax
        mov ah,2
        int 21h
        pop ax
        ret
;-------------------------
prtnum: pusha
        xor si,si
        xor dx,dx
        mov cx,10
.start: div cx
        push dx
        xor dx,dx
        inc si
        test ax,ax
            jz .prt
        jmp .start
.prt:   dec si
        pop ax
        add al,30h
        mov dl,al
        call prtc
        test si,si
            jz .done
        jmp .prt
.done:  popa
        ret
;-------------------------
exit:   mov ah,1
        int 21h
        mov ah,4ch
        int 21h

chr db 20 dup(?),0
chr2 db 24 dup(61h)    


Output:
Code:
welcome to fasm
b=1
b=1
c=2
b=1
b=1
c=2
c=2
b=1
b=1
b=1    
Post 03 May 2014, 16:34
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 03 May 2014, 16:43
fasmnewbie wrote:
MariaSM

Seems like it may take you forever to understand revolution and ASMGuru here. I modified this code from my library and I intentionally leave one part buggy for you to figure out. It deals only with lower case characters. Learn from it and read carefully of what Guru and revolution are saying. Take your time.

Code:
org 100h
        xor si,si
;------------------------
get:    call get$
        cmp al,0dh
            je ok
        mov [chr+si],al
        sub al,61h
        movzx di,al
        add [chr2+di],1
        inc si
        cmp si,20
            je ok
        jmp get
ok:     call nline
        xor di,di
;-------------------------
done:   cmp [chr2+di],'a'
            ja change
        inc di
good:   cmp di,24
            je exit
        jmp done
change: xor ax,ax
        mov al,[chr2+di]
        sub al,61h
        mov dl,[chr2+di]
        call prtc
        mov dl,'='
        call prtc
        call prtnum
        call nline
        inc di
        cmp [chr2+di],'a'
            ja change
        jmp good
;--------------------------
get$:   mov ah,1
        int 21h
        ret
nline:  mov dl,0dh
        call prtc
        mov dl,0ah
        call prtc
        ret
prtc:   push ax
        mov ah,2
        int 21h
        pop ax
        ret
;-------------------------
prtnum: pusha
        xor si,si
        xor dx,dx
        mov cx,10
.start: div cx
        push dx
        xor dx,dx
        inc si
        test ax,ax
            jz .prt
        jmp .start
.prt:   dec si
        pop ax
        add al,30h
        mov dl,al
        call prtc
        test si,si
            jz .done
        jmp .prt
.done:  popa
        ret
;-------------------------
exit:   mov ah,1
        int 21h
        mov ah,4ch
        int 21h

chr db 20 dup(?),0
chr2 db 24 dup(61h)    


Output:
Code:
welcome to fasm
b=1
b=1
c=2
b=1
b=1
c=2
c=2
b=1
b=1
b=1    

I don't understant the output. What is "b=1 b=1..."?
Post 03 May 2014, 16:43
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3, 4  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.