News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Compaire two string inputs help ?

Started by mimime, March 20, 2007, 02:46:14 PM

Previous topic - Next topic

mimime

Hi I am new to ASM;

I need some help, I am trying to write a program that takes two strings as input and compair them then gives a message.

I wrote the program but I have a porbelm I will explain what I wrote then I will write the problem:

-first I ask for the lengh of the string and I save it on cx( done)
-then I input the first string and save it using push (done)
-second I input the second string and save it on memory location (here I have the problem ) I will write the code for this one
   mov   AH, 9              ;
   mov   DX, OFFSET Now   ;Display the message ""enter your name""
   int   21h 
   
   mov si,400h
   mov cx,di                   ; I get the value of the counter to match
                                   ;the lengh of the ref string lengh
    mov bx,2h                  ; I set the value of bx to 2 to use it in mem shift
num:       
    mov ah,01h                  ; now we enter the name from keyboard
    int 21h
     
    mov [si],ax                 ; the data on ax from keyboard is saved on mem
                                    ; on mem location of SI
    push [si]                    ; another try of me to get the start address of the string
    lea si,[800h+bx]           ; I saved the effective address in SI
    add bx,02h                  ; we need to shift the mem addres two byte         
    dec cx
    jnz num                      ; keep getting more charaters till cx is zero

----------------------------------------------------------------------
so far its good now I have two strings one saved using push and the other one is saved by mem location saved on SI (after using pop command )or the last charater of the string is saved on memory locatoin SI (the current value of SI)
------------------------------------------------------------------------
for some reason I can not make the pointer to point to the first charater of the string I saved on the memory location neither using pop or I made a small commads to try to decrement the pointer to point first charater
here is what i have done
I tried to decrement [si] twice
did not work....

I need your help on understand how to make the pointer points to the first charater I have saved on the memory ... the probelm I can not push IP or CS ?

any help is really appriated this is my first programe and I spend 3 days try to solove this problem but no use


By the way I am not allowed to buffer the input of the string ... that I did on the first porject but I am not allowed to buffer the input  :'(

Regards,
Alan

MichaelW

Hi Alan,

I do not understand what you mean by "I am not allowed to buffer the input of the string". To compare two input strings, you must store (buffer) at least one of them. Whether you are storing a string by pushing the characters onto the stack, or by moving them into a buffer, you are storing the string in a memory buffer. The only real differences are the location of the memory buffer and the instructions that you use to store the characters. I'm going to assume that pushing them onto the stack is allowed. I am also not sure exactly what you are trying to do with your code, so I will try to follow your description instead.

Interrupt 21h Function 01h returns the characters in AL, and to save the characters onto the stack you can push AX directly, but you should probably clear AH before you push AX.

Assuming that the first string had been pushed onto the stack, I think the easiest method of handling the second string would be to push it onto the stack as well. To compare the strings you could treat the stack as a memory buffer, setting SI to the start of one string and DI to the start of the other. To do this you would need to know how the stack operates. In 16-bit code, the push instruction subtracts 2 from SP and then copies its operand to SS:SP, and the pop instruction copies the value at SS:SP to its operand and then adds 2 to SP. The important points are that the stack "grows" down in memory, and that SP always points to the last value pushed. So after both strings had been pushed, SP would point to the last character of the second string. Knowing the lengths of the strings, and that each character is stored in a 2-byte word, setting up SI and DI should be no problem. You could perform comparison in a loop, doing the comparisons and adjusting SI and DI, or with a REP CMPSW instruction.
eschew obfuscation

mimime

MichaelW , thanks a lot for your reply .. I will try to explain better
- I already wrote a program using int 21,0A which buffer the keyboard but now the treacher think that was kind of playing smart.
- I have two inputs lets say NameA and NameB

I ask the use to input namea then i save it and I save the lenght of it, then i ask him again to input nameb (i use the lengh of namea to limit the name size)

I first save in namea in a stack uinsg push ( later i will retrive this name using pop)
i saved nameb on a memory location

now I need to compair the two names so for the first one which namea is easy to get the data using pops
for the second nameb my pointer is pointing to the last charater saved of the name lets say its letter b .... now I need to know how get the first address of nameb which lets say letter n ... having the addrees of the last letter which is p..... I tried pushing the effective address when I first start saving nameb (did not work) I tried then decrementing the address that I have also did not work ....

All I need to be able to point to the first charater of the second name whcih is nameb having the address of the last letter whcih letter b
------------------
I understand what you said about saving both names on the same string , but I am not sure I will be able to know how figure the address of each charater

mimime


PBrennick

mimime,

Remember, if you know the length of the string all you have to do is subtract that value from the pointer register to get to the start. also, if you store both strings as one then you can do the compare by, again using the length. If you are using SI and the length of the string is 10 then it would be:

mov al, [si]
cmp [si+10], al

and branch accordingly.

This saves one register for other use and registers are scarce.
Paul
The GeneSys Project is available from:
The Repository or My crappy website

eek

It sounds to me like your schoolteacher is probably looking for you to produce a cmpsb solution.