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 |
|
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 |
|||
03 May 2014, 10:18 |
|
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 |
|||
03 May 2014, 10:31 |
|
revolution 03 May 2014, 10:45
I am having trouble parsing this instruction:
Code: LEA DX,CL |
|||
03 May 2014, 10:45 |
|
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..
|
|||
03 May 2014, 10:48 |
|
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. |
|||
03 May 2014, 11:01 |
|
MariaSM 03 May 2014, 11:10
I replaced that part but it doesn't display anything
how about counting and comparing? is this ok? |
|||
03 May 2014, 11:10 |
|
MariaSM 03 May 2014, 12:00
but al and cl doesn't need to have 8bits each other?
|
|||
03 May 2014, 12:00 |
|
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. |
|||
03 May 2014, 12:08 |
|
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.
|
|||
03 May 2014, 12:26 |
|
MariaSM 03 May 2014, 13:00
so..it is or not okay the comp and counting?
|
|||
03 May 2014, 13:00 |
|
AsmGuru62 03 May 2014, 13:19
What exactly is "comp and counting"?
|
|||
03 May 2014, 13:19 |
|
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 |
|||
03 May 2014, 13:57 |
|
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 ; |
|||
03 May 2014, 14:45 |
|
MariaSM 03 May 2014, 15:51
AsmGuru62 wrote: I would suggest this: thanks. and what should I do to display what it is in AH? |
|||
03 May 2014, 15:51 |
|
MariaSM 03 May 2014, 16:01
And one another problem. why I can't read more than 12 charcater ?
|
|||
03 May 2014, 16:01 |
|
revolution 03 May 2014, 16:08
MariaSM wrote: And one another problem. why I can't read more than 12 charcater ? |
|||
03 May 2014, 16:08 |
|
MariaSM 03 May 2014, 16:18
revolution wrote:
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.. |
|||
03 May 2014, 16:18 |
|
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 |
|||
03 May 2014, 16:34 |
|
MariaSM 03 May 2014, 16:43
fasmnewbie wrote: MariaSM I don't understant the output. What is "b=1 b=1..."? |
|||
03 May 2014, 16:43 |
|
Goto page 1, 2, 3, 4 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.