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
Author
Thread Post new topic Reply to topic
AsmGuru62



Joined: 28 Jan 2004
Posts: 1670
Location: Toronto, Canada
AsmGuru62 06 May 2014, 21:39
If the code worked ok when I posted it (I tested it before posting, but it was FASM code), then something happened during the transfer from FASM to MASM. So, what changes did you make to make it into MASM?
Post 06 May 2014, 21:39
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 06 May 2014, 22:03
AsmGuru62 wrote:
If the code worked ok when I posted it (I tested it before posting, but it was FASM code), then something happened during the transfer from FASM to MASM. So, what changes did you make to make it into MASM?

I remove PUSHA and POPA from PRINT_AH functions and that's all that I've modified...
Post 06 May 2014, 22:03
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1670
Location: Toronto, Canada
AsmGuru62 06 May 2014, 22:45
I see... do these instruction not passing the assembly?
If so, try to use ".386" directive.
Post 06 May 2014, 22:45
View user's profile Send private message Send e-mail Reply with quote
zhak



Joined: 12 Apr 2005
Posts: 501
Location: Belarus
zhak 06 May 2014, 23:01
Shocked Mother of God!! What a thread!! MariaSM should definitely get A+++ for her homework Very Happy
Post 06 May 2014, 23:01
View user's profile Send private message Reply with quote
nop



Joined: 01 Sep 2008
Posts: 165
Location: right here left there
nop 07 May 2014, 04:17
haha yes more than 60 replies in 3 days like bees arond a honey pot i bet mariasm not realy a girl but evry one just asumes it Laughing
Post 07 May 2014, 04:17
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 07 May 2014, 14:06
but why we use print_ah like a function and not in program?
and why when you check the length of the string you compare it with 1? Very Happy
Code:
cmp inlen, 1    
Post 07 May 2014, 14:06
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1670
Location: Toronto, Canada
AsmGuru62 07 May 2014, 14:29
I am not sure I understand the question.
The program needs to print values few times, like print a count and then positions.
The printing is done with the same code, so the same code must be repeated in a places you need it to be.
This makes code increase in size (because you need to copy these lines around) and makes the code hard to read.
Functions are to make code easy to read for years to come.
Sometimes function is made just to organize the code, even if that code is repeated only once.
With that approach your code would have looked like that:
Code:
CALL INPUT_TEXT
CALL INPUT_CHAR
CALL FIND_ALL_CHARS
CALL PRINT_COUNT
CALL PRINT_POSITIONS

MOV AX, 4C00h
INT 21H
;
; YOUR FUNCTIONS...
;
INPUT_TEXT:
; ask user for a string and accept input
RET

INPUT_CHAR:
; ask user for a character and accept it
RET

FIND_ALL_CHARS:
; scan the entered text and store all found positions into array
RET

PRINT_COUNT:
; print the count of found characters
RET

PRINT_POSITIONS:
; if count of found characters > 0 then print all positions
RET
    
Post 07 May 2014, 14:29
View user's profile Send private message Send e-mail Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1670
Location: Toronto, Canada
AsmGuru62 07 May 2014, 14:32
When AH=0AH service is used to enter a string -- the 0D character is counted in the 2nd byte of the buffer (1st byte is a max. len + 1).
So, if you enter an empty string - the value in the byte is 1, because text
has no characters, just the terminating 0D.
If you enter "TEST" - this byte will count 5 - 4 characters entered plus 0D.
1 in the 2nd byte means empty string.
Post 07 May 2014, 14:32
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 07 May 2014, 14:34
thanks Smile
i understood
Post 07 May 2014, 14:34
View user's profile Send private message Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 07 May 2014, 15:35
two more questions.
can you explain me this instructions?
Code:
MOV AX, SI
SUB AX, OFFSET S1     
why offset s1?

and here
Code:
MOV DI, OFFSET ARRPOS
ADD DI, ARRCOUNT
MOV [DI], AL
INC ARRCOUNT     
why do you move al in [di]?


Last edited by MariaSM on 07 May 2014, 16:47; edited 1 time in total
Post 07 May 2014, 15:35
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 07 May 2014, 16:27
MariaSM wrote:
why do you move al in di?


Not in "di" but in "[di]". Feel the difference.

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9
Post 07 May 2014, 16:27
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1670
Location: Toronto, Canada
AsmGuru62 07 May 2014, 17:04
Here it is visually explained:
Code:
OFFSET S1
|
+---+---+---+---+---+---+---+---+---+---+---+-----+
| H | E | L | L | O |   | K | I | T | T | Y | 0Dh |
+---+---+---+---+---+---+---+---+---+---+---+-----+
                                |
                                SI (points AFTER found character)

AX = SI - OFFSET S1

^^^ It calculates the position of the found character (relative to S1)
    

Since the position will never be more than 20, AH will be always zero, so only AL is stored into array of positions.
Post 07 May 2014, 17:04
View user's profile Send private message Send e-mail Reply with quote
MariaSM



Joined: 03 May 2014
Posts: 41
MariaSM 08 May 2014, 11:20
AsmGuru62 wrote:
Here it is visually explained:
Code:
OFFSET S1
|
+---+---+---+---+---+---+---+---+---+---+---+-----+
| H | E | L | L | O |   | K | I | T | T | Y | 0Dh |
+---+---+---+---+---+---+---+---+---+---+---+-----+
                                |
                                SI (points AFTER found character)
AX = SI - OFFSET S1
^^^ It calculates the position of the found character (relative to S1)
    

Since the position will never be more than 20, AH will be always zero, so only AL is stored into array of positions.

I still don't get it. Could you,please, give me an example with offset?
Why is AH is always 0 and what exactly contain AL?
And why the PRINT_AH function is called at positions displaying? I mean here:
Code:
MOV AH, [SI]
INC SI
CALL PRINT_AH
LOOP PRINT_POS    
Post 08 May 2014, 11:20
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1670
Location: Toronto, Canada
AsmGuru62 08 May 2014, 15:12
I think you have used OFFSET in your code.
OFFSET means ADDRESS in a context of this program.
So, expression OFFSET S1 means ADDRESS of 1st byte of variable S1.
I am not sure how to explain it better.

This expression: AX = SI - OFFSET S1 -- calculates the position of the character.
This position in AX cannot be greater than 20, because you cannot enter >20 characters.
The register AX contains two smaller 8-bit registers: AH and AL:
Code:
+--------+--------+
|   AH   |   AL   |
+--------+--------+
|                 |
|<--     AX    -->|
    

Now, if you put values from 1 to 20 into AX, those values will fill AL, but AH will be set to 0.

Let me put some comments on the 2nd part of your question:
Code:
MOV     AH, [SI]        ; Load position (because SI was set to an array of positions) into AH
INC     SI              ; Now SI points to next position in the array
CALL    PRINT_AH        ; Print whatever is in AH (and that is a position from array)
    

I suggest you get a debugger and step through your code.
It helps to understand the code better.
Try it:
http://wetolearn.blogspot.ca/2013/10/8086-assembly-debugging-with-afd-advance.html
Post 08 May 2014, 15:12
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

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

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.