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
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:25
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Then you need an array of positions and a counter of how many stored:
Code:
ARRPOS DB 20 DUP (0)
ARRCOUNT DW 0
    

Next you need to make the code which prints AH into a function:
Code:
PRINT_AH:
        PUSHA
        ... move the code lines for printing AH here ...
        POPA
        RET
    

Insert that whole code after exiting to DOS, but before the data is declared:
Code:
...
MOV AX, 4C00h
INT 21h

<-- INSERT THE CODE FOR "PRINT_AH" FUNCTION HERE !

STRING DB ...
... etc. data declared
    

Now, when the character is found -- you count it, so immedeately after that you need to calculate and store
the position into that array:
Code:
;
; Preserve registers we going to use
;
PUSH DI
PUSH AX
;
; POS = SI minus address of STRING
;
MOV AX, SI
SUB AX, OFFSET STRING
;
; Store AL into ARRPOS [ARRCOUNT]
;
MOV DI, OFFSET ARRPOS
ADD DI, ARRCOUNT
MOV [DI], AL
INC ARRCOUNT
;
; Restore used registers (they may be needed later in code)
;
POP AX
POP DI
    

It is possible that this code ^^^ is also nice to put into a function to make your code easier to read.

After the process, you just need to print all values from ARRPOS array:
Code:
MOV CX, ARRCOUNT
MOV SI, ARRPOS

PRINT_POS:

CMP CX, ARRCOUNT
JE DO_PRINT_POS

MOV DX, ", "   ; <-- declare it and use the name
MOV AH, 9
INT 21H

DO_PRINT_POS:
MOV AH, [SI]
INC SI
CALL PRINT_AH
LOOP PRINT_POS

; print 10,13,"$"
    
Post 05 May 2014, 15:25
View user's profile Send private message Send e-mail Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
I'm totally confused after reading the entire topic so I'll follow what the header says.
Quote:
Count the number of a particular character in a string

Code:
; Usage : 
; E(SI) - Pointer to string
; BL - Character
; OUT: E(CX) number of characters
count_num_of_chars_in_string:
; save registers
pushfd ; save flags will be altered by cmp 
push eax
push esi
xor ECX, ECX ; make ECX 0
.loop:
lodsb
cmp al, bl
je .found_char
cmp al, 0
je .done
jmp .loop
.found_char:
inc ECX
jmp .loop
.done:
pop esi
pop eax
popfd
ret

    

Untested. Smile

_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD
Post 05 May 2014, 15:40
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Oh, My!..
Your post adds some more confusion!.. good work, however.

P.S. The code below actually compiles!.. wow!.. FASM is awesome! Too forgiving.
A coder who uses code label ".loop:" must be punished!
Code:
.loop:
  loop .loop
    
Post 05 May 2014, 16:58
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
Then you need an array of positions and a counter of how many stored:
Code:
ARRPOS DB 20 DUP (0)
ARRCOUNT DW 0
    

Next you need to make the code which prints AH into a function:
Code:
PRINT_AH:
        PUSHA
        ... move the code lines for printing AH here ...
        POPA
        RET
    

Insert that whole code after exiting to DOS, but before the data is declared:
Code:
...
MOV AX, 4C00h
INT 21h

<-- INSERT THE CODE FOR "PRINT_AH" FUNCTION HERE !

STRING DB ...
... etc. data declared
    

Now, when the character is found -- you count it, so immedeately after that you need to calculate and store
the position into that array:
Code:
;
; Preserve registers we going to use
;
PUSH DI
PUSH AX
;
; POS = SI minus address of STRING
;
MOV AX, SI
SUB AX, OFFSET STRING
;
; Store AL into ARRPOS [ARRCOUNT]
;
MOV DI, OFFSET ARRPOS
ADD DI, ARRCOUNT
MOV [DI], AL
INC ARRCOUNT
;
; Restore used registers (they may be needed later in code)
;
POP AX
POP DI
    

It is possible that this code ^^^ is also nice to put into a function to make your code easier to read.

After the process, you just need to print all values from ARRPOS array:
Code:
MOV CX, ARRCOUNT
MOV SI, ARRPOS

PRINT_POS:

CMP CX, ARRCOUNT
JE DO_PRINT_POS

MOV DX, ", "   ; <-- declare it and use the name
MOV AH, 9
INT 21H

DO_PRINT_POS:
MOV AH, [SI]
INC SI
CALL PRINT_AH
LOOP PRINT_POS

; print 10,13,"$"
    


It gives me a error at
Code:
MOV SI, ARRPOS    
Post 05 May 2014, 18:48
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Oh... right, in MASM you use OFFSET in front of the name. Just use OFFSET ARRPOS.
Post 05 May 2014, 18:53
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
Oh... right, in MASM you use OFFSET in front of the name. Just use OFFSET ARRPOS.



can you help me with the entire program?
after I read the string and the character I receive a error: "16bit MS-DOS subsystem"

I attached the program


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



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
I have FASM, and you use MASM.
I can try however...
Post 05 May 2014, 19:49
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
I have FASM, and you use MASM.
I can try however...

thanks a lot!
Post 05 May 2014, 19:53
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
It is really funny how strongly impacts a female nickname in an assembly programmers forum... Very Happy
Here we have even a "photo" of MariaSM, but the stack overflow users are not so naive.
Post 05 May 2014, 21:23
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1419
Location: Toronto, Canada
AsmGuru62
Actually, I'll help anyone, female or not.
Please take a look at the code:


Description:
Download
Filename: DosCom.Asm
Filesize: 3.77 KB
Downloaded: 145 Time(s)

Post 05 May 2014, 21:36
View user's profile Send private message Send e-mail Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie
AsmGuru62 wrote:
Actually, I'll help anyone, female or not.
Please take a look at the code:


Guru, I copied your code for demonstration. Hope its ok Razz

http://board.flatassembler.net/topic.php?t=16708
Post 05 May 2014, 21:53
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie
MariaSM wrote:
AsmGuru62 wrote:
I have FASM, and you use MASM.
I can try however...

thanks a lot!


Maria, do you need anything else? :p
Post 05 May 2014, 21:55
View user's profile Send private message Visit poster's website Reply with quote
fasmnewbie



Joined: 01 Mar 2011
Posts: 555
fasmnewbie
JohnFound wrote:
It is really funny how strongly impacts a female nickname in an assembly programmers forum... Very Happy
Here we have even a "photo" of MariaSM, but the stack overflow users are not so naive.


I don't know man. Guru never helped me this much. hehehe Very Happy
Post 05 May 2014, 21:57
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
AsmGuru62 wrote:
A coder who uses code label ".loop:" must be punished!
Code:
.loop:
  loop .loop
    
Why? I use .loop regularly. I rarely use loop though. Let's loop the loop.
Post 05 May 2014, 23:35
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17714
Location: In your JS exploiting you and your system
revolution
JohnFound wrote:
It is really funny how strongly impacts a female nickname in an assembly programmers forum... Very Happy
Here we have even a "photo" of MariaSM, but the stack overflow users are not so naive.
Perhaps it doesn't matter if the photo is real or not. Just the idea seems to be enough to elicit more positive responses. Maybe you should change your name to JaneFound and get more help with Fresh? Wink And maybe I should change my name to SexyNerdyChick and post a picture of Natalie Portman? Shocked
Post 06 May 2014, 02:05
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3502
Location: Bulgaria
JohnFound
revolution wrote:
Perhaps it doesn't matter if the photo is real or not. Just the idea seems to be enough to elicit more positive responses. Maybe you should change your name to JaneFound and get more help with Fresh? Wink And maybe I should change my name to SexyNerdyChick and post a picture of Natalie Portman? Shocked


Yes, indeed. Very Happy JaneFound - I like it. But no, thanks. Wink
BTW, the serious assembly girls actually take male nicknames, or neutral.

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 06 May 2014, 05:19
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
AsmGuru62 wrote:
Actually, I'll help anyone, female or not.
Please take a look at the code:


thank you very much! I really appreciate what you did for me!

this program have some bugs..after displaying the correct positions, it displays many other numbers..


Last edited by MariaSM on 06 May 2014, 19:29; edited 1 time in total
Post 06 May 2014, 06:01
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
count every occurences of every ascii chars, stop on the 0 char

Code:
count:
times dd 0
...
countChars:
;esi = input asciiZ string
.loop:
    movzx eax,byte[esi]
    or al,al
    je .end:
    inc dword[counts+eax*4]
    inc esi
    jmp .loop
.end:
    ret
...
getCount:
;al = ascii code to count
;return eax = count    
    movzx eax,al
    mov eax,[counts+eax*4]
    ret
...
    


porting this code to 16 bits should not be very hard
Post 06 May 2014, 09:52
View user's profile Send private message Visit poster's website Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM
MariaSM wrote:
AsmGuru62 wrote:
Actually, I'll help anyone, female or not.
Please take a look at the code:


thank you very much! I really appreciate what you did for me!

this program have some bugs..after displaying the correct positions, it displays many other numbers..

I have tried for one or two hours to find what's wrong with the program , but I don't find what doesn't work properly...some help?
Post 06 May 2014, 19:32
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 GitHub, YouTube, Twitter.

Website powered by rwasa.