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 |
|
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... |
|||
06 May 2014, 22:03 |
|
AsmGuru62 06 May 2014, 22:45
I see... do these instruction not passing the assembly?
If so, try to use ".386" directive. |
|||
06 May 2014, 22:45 |
|
zhak 06 May 2014, 23:01
Mother of God!! What a thread!! MariaSM should definitely get A+++ for her homework
|
|||
06 May 2014, 23:01 |
|
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
|
|||
07 May 2014, 04:17 |
|
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? Code: cmp inlen, 1 |
|||
07 May 2014, 14:06 |
|
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 |
|||
07 May 2014, 14:29 |
|
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. |
|||
07 May 2014, 14:32 |
|
MariaSM 07 May 2014, 14:34
thanks
i understood |
|||
07 May 2014, 14:34 |
|
MariaSM 07 May 2014, 15:35
two more questions.
can you explain me this instructions? Code: MOV AX, SI SUB AX, OFFSET S1 and here Code: MOV DI, OFFSET ARRPOS ADD DI, ARRCOUNT MOV [DI], AL INC ARRCOUNT Last edited by MariaSM on 07 May 2014, 16:47; edited 1 time in total |
|||
07 May 2014, 15:35 |
|
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 |
|||
07 May 2014, 16:27 |
|
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. |
|||
07 May 2014, 17:04 |
|
MariaSM 08 May 2014, 11:20
AsmGuru62 wrote: Here it is visually explained: 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 |
|||
08 May 2014, 11:20 |
|
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 |
|||
08 May 2014, 15:12 |
|
Goto page Previous 1, 2, 3, 4 < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.