flat assembler
Message board for the users of flat assembler.

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

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



Joined: 01 Mar 2011
Posts: 553
fasmnewbie
MariaSM wrote:
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..."?


That's the bug I told you about. It should display the character and the number of times it appears in the string you entered.

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



Joined: 03 May 2014
Posts: 41
MariaSM
fasmnewbie wrote:
MariaSM wrote:
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..."?


That's the bug I told you about. It should display the character and the number of times it appears in the string you entered.

Code:
welcome to fasm
a=1
c=1
e=2
...    

sorry but I have some problems with my code.if you can help me with this, i would remain grateful
i don't want to solve other problem with a different code
Post 03 May 2014, 16:55
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
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.

oh.i didn't seen that.thanks.now I understood that
Post 03 May 2014, 17:01
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 553
fasmnewbie
MariaSM wrote:
sorry but I have some problems with my code.if you can help me with this, i would remain grateful
i don't want to solve other problem with a different code
It is not about the code. Check the algorithm of solving the problem particularly in converting integer to string. As I see it, that's your biggest problem and that's what revolution meant by 'DIV' instruction. Why don't you convert that to TASM because I am no TASM guy. Put the thing to convert in AX and call the similar putnum procedure in TASM. That solves 50% of your problem.
Post 03 May 2014, 17:05
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
fasmnewbie wrote:
MariaSM wrote:
sorry but I have some problems with my code.if you can help me with this, i would remain grateful
i don't want to solve other problem with a different code
It is not about the code. Check the algorithm of solving the problem particularly in converting integer to string. As I see it, that's your biggest problem and that's what revolution meant by 'DIV' instruction. Why don't you convert that to TASM because I am no TASM guy. Put the thing to convert in AX and call the similar putnum procedure in TASM. That solves 50% of your problem.



yes, I've understood what the purpose was..but I don't really have much time to make this program works.and I really appreciate if someone can say me, how i can do to display ah content
Post 03 May 2014, 17:33
View user's profile Send private message Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 553
fasmnewbie
MariaSM wrote:
yes, I've understood what the purpose was..but I don't really have much time to make this program works.and I really appreciate if someone can say me, how i can do to display ah content


Code:
mov ah,9 ;want to display '9'?
mov dl,ah
add dl,30h
mov ah,2
int 21h    


Its all there in the code I gave you (prtc and prtnum). You just need to look harder.
Post 03 May 2014, 17:50
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
i will do this.but i don't have time now..
for the next program i promise that i'll be more prepared'but now i really need your help
Post 03 May 2014, 18:45
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
Is this a homework assignment? People who complain about having limited time for some simple task that has no real word usage trigger this question in my head.
Post 03 May 2014, 23:25
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
yes this is a part if one homework...
Post 04 May 2014, 10:12
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1411
Location: Toronto, Canada
AsmGuru62
If AH contains values from 0 to 20, then try this code:
Code:
cmp al, 0
je print_zero
;
; Divide AH by 10
;
shr ax, 8
mov dx, 0
mov cx, 10
div cx
;
; DL is a remainder of division, AL is a result.
; Ex.: If you have 17 and divide by 10 - remainder is 7, and result is 1
; 1st you print the result and then remainder:
;
push dx
add al, '0'
mov dl, al
mov ah, 2
int 21h
;
; print remainder
;
pop dx
add dl, '0'
int 21h
jmp all_done

print_zero:
;
; print the text "0$", like you do with other strings
;



all_done:
;
; ...
;
    
Post 04 May 2014, 12:41
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
If AH contains values from 0 to 20, then try this code:
Code:
cmp al, 0
je print_zero
;
; Divide AH by 10
;
shr ax, 8
mov dx, 0
mov cx, 10
div cx
;
; DL is a remainder of division, AL is a result.
; Ex.: If you have 17 and divide by 10 - remainder is 7, and result is 1
; 1st you print the result and then remainder:
;
push dx
add al, '0'
mov dl, al
mov ah, 2
int 21h
;
; print remainder
;
pop dx
add dl, '0'
int 21h
jmp all_done

print_zero:
;
; print the text "0$", like you do with other strings
;



all_done:
;
; ...
;
    


At any input, the output is "09"
Post 04 May 2014, 13:50
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1411
Location: Toronto, Canada
AsmGuru62
I made a mistake there (my apologies).
But you still not understanding the code, and you need to.
If you would have read the code - you would have found my mistake.
You act like we have to write the code for your homework.
The mistake is in the first line:
Code:
cmp al, 0    

it must be:
Code:
cmp ah, 0    
Post 04 May 2014, 14:22
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
I made a mistake there (my apologies).
But you still not understanding the code, and you need to.
If you would have read the code - you would have found my mistake.
You act like we have to write the code for your homework.
The mistake is in the first line:
Code:
cmp al, 0    

it must be:
Code:
cmp ah, 0    


I corrected this part. i understood why you compared with 0..
but the output is still 0.
is it possible to be something wrong at comparison?
Post 04 May 2014, 14:32
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1411
Location: Toronto, Canada
AsmGuru62
I never debugged the code, it is possible that it has other bugs.
Can you post your whole code here?
Post 04 May 2014, 14:38
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
yeah,sure


Last edited by MariaSM on 07 May 2014, 13:07; edited 2 times in total
Post 04 May 2014, 14:51
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1411
Location: Toronto, Canada
AsmGuru62
To be consistent you may use MOV instead of LEA:
Code:
LEA SI, STRING
    

is same as:
Code:
MOV SI, OFFSET STRING     ; like you do in other places
    

But that is not a bug.
You are using MASM, and I use FASM, so I need some time to look into it.

Issue #2:
The incorrect use of function 0AH - DX must point to a following data:
Code:
+---------+---------+-----------------------+
| MAXIMUM | ENTERED | STRING ENTERED ...    |
+---------+---------+-----------------------+
|
DX
    

In your code DX is always pointing to a STRING - not correct.
I think I posted the correct code in some post above.

When you ask user to enter 20 characters - MAXIMUM byte must be 20 (or even 21 to counf the 0Dh)
Of course, when you ask for a single character - MAXIMUM must be 1 (or 2)


Description:
Download
Filename: ChCount.Asm
Filesize: 5 KB
Downloaded: 130 Time(s)

Post 04 May 2014, 15:06
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
To be consistent you may use MOV instead of LEA:
Code:
LEA SI, STRING
    

is same as:
Code:
MOV SI, OFFSET STRING     ; like you do in other places
    

But that is not a bug.
You are using MASM, and I use FASM, so I need some time to look into it.

Issue #2:
The incorrect use of function 0AH - DX must point to a following data:
Code:
+---------+---------+-----------------------+
| MAXIMUM | ENTERED | STRING ENTERED ...    |
+---------+---------+-----------------------+
|
DX
    

In your code DX is always pointing to a STRING - not correct.
I think I posted the correct code in some post above.

When you ask user to enter 20 characters - MAXIMUM byte must be 20 (or even 21 to counf the 0Dh)
Of course, when you ask for a single character - MAXIMUM must be 1 (or 2)


Last edited by MariaSM on 07 May 2014, 13:07; edited 2 times in total
Post 04 May 2014, 17:35
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1411
Location: Toronto, Canada
AsmGuru62
So, you need to display positions AFTER showing the # of times the character was found?
Like this?
Code:
String: ABCDEFGH-HELLO
Character: E
Count: 2
Positions: 5, 11
    

or like that:
Code:
String: ABCDEFGH-HELLO
Character: E
- found at position: 5
- found at position: 11
Count: 2
    

You see what I mean?
You can report positions DURING a search or you can report them all at once AFTER the search.

In 1st case - you just need to report the following:

POS = (SI <where char is found> minus STRING) + 1

In 2nd case you need to store these positions into
an array and then print them all AFTER search. In that case, you need an array and a counter of how many you have in that array.

Also, you probably need a function, which prints a value.
Currently it is not a function you can call - it is just some lines of code.
If you find that you need to repeat some code lines - it is a sign that you need a function for these lines.

A function is basically a label and some code after it and then RET instruction:
Code:
PRINT_AH:
... do some stuff ...
RET
    

and then you call the function:
Code:
MOV AH, 17
CALL PRINT_AH     ; It will print "17" (value in AH) to the console
    
Post 04 May 2014, 23:17
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
like that
Code:
String: ABCDEFGH-HELLO
Character: E
Count: 2
Positions: 5, 11
    

Post 05 May 2014, 05:23
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
how can I make this? where I'm supposed to put these instructions in the program?
and about pos? how could I declare it?
Post 05 May 2014, 15:23
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  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-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.