News:

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

String compare issue

Started by Nilrem, February 03, 2005, 10:28:48 PM

Previous topic - Next topic

Nilrem

I am using this code to determine valid user input, but if I just hit enter (entering no user input) it prints done when it shouldn't.

@@: mov lpstring,input (13,10,"Run again? Y/N: ")
xor edx,edx
mov edx,[lpstring]
xor eax,eax
cmp edx,eax
je @b
print chr$("done")
;test eax,eax
;jnz @f
;print chr$(13,10,"That is not valid")
mov lpstring, input (13,10,"Press Enter to Exit")

Thanks once more.

Mirno

You are comparing the pointer to the string to zero, not what the pointer is pointing to.


@@:
  mov lpstring, input (13, 10, "Run again? Y/N: ")
  mov edx, lpstring
  cmp BYTE PTR [edx], 0
  je @B


Mirno

Nilrem

Thankyou, how do I convert to uppercase? Similar to "TOUPPER"?
Thankyou.

Tedd

check that each of the characters is >= 'a' and <= 'z'
if you find one that is then it's lower case and should be made uppercase
simple way is to subtract 32

for any other characters, they're either already uppercase (so no need to change them) or they're not alphabetic anyway.
No snowflake in an avalanche feels responsible.

Nilrem

#4
.if BYTE PTR [eax] >= 'a' and BYTE PTR [eax] >= 'z' doesn't work, actually I would prefer to write it in the form of cmp eax,eax etc, however it would seem to take up less lines if I did it by the above method.

I have done this:

mov eax, [lpstring]
.if BYTE PTR [eax] >= 'a'
.if BYTE PTR [eax] <= 'z'
sub eax,32
.endif
.endif

cmp BYTE PTR [edx], al
je @b
mov [lpstring], eax
print lpstring
;test eax,eax
;jnz @f
;print chr$(13,10,"That is not valid")
mov lpstring, input (13,10,"Press Enter to Exit")

Unfortunately this prints hex.

Tedd

I was just saying how to do it - that wasn't meant to be code :P

Anyway, I think you can do:
.IF ((BYTE PTR [eax] >= 'a') && (BYTE PTR [eax] <= 'z'))
No snowflake in an avalanche feels responsible.

Nilrem

Hehe, well it works, but it prints out in HEX.

MichaelW

Nilrem,

I'm not sure what you mean by prints out in hex, but to convert a lower case character to upper case you need to subtract 32 from the character code, so:

sub  eax,32

Should be:

sub  BYTE PTR[eax],32

But if just two responses are possible, why bother to convert the case? You can just test for all possible valid inputs, for example:

    .IF BYTE PTR [eax] == 'Y' || BYTE PTR [eax] == 'y'
      print chr$("yes")
    .ELSEIF BYTE PTR [eax] == 'N' || BYTE PTR [eax] == 'n'
      print chr$("no")
    .ELSE
      print chr$("try again")
      jmp   @B
    .ENDIF


Also, the input macro has a problem when it is called from a loop:

http://www.masmforum.com/simple/index.php?topic=434.0

AFAIK the update for this problem is not available yet. You can fix the problem locally by copying the macro to your source file, renaming it so it will not conflict with the original macro, applying the fix, and then modifying your source to call the fixed macro. Here is a fixed and renamed version:

    _input MACRO prompt:VARARG
        LOCAL txt
        LOCAL buffer
      IFNB <prompt>
        .data
          txt db prompt, 0       ;; <===
          buffer db 128 dup (0)
          align 4
        .code
        invoke StdOut,ADDR txt
        invoke StdIn,ADDR buffer,LENGTHOF buffer
        invoke StripLF,ADDR buffer
        mov eax, offset buffer
        EXITM <eax>
      ELSE
        .data
          buffer db 128 dup (0)
          align 4
        .code
        invoke StdIn,ADDR buffer,LENGTHOF buffer
        invoke StripLF,ADDR buffer
        mov eax, offset buffer
        EXITM <eax>
      ENDIF
    ENDM

eschew obfuscation

Nilrem

Here is my finished result if anyone is interested, simple but, thought should post it for all the help I've recieved.

[attachment deleted by admin]

pbrennick

Nilrem,
You have worked out the problems you posted about.  Now you need to fix the fact that it does not return volume serial numbers.  On my machine it just resturns 00000000 for hex and 0 for dec.  Definitely not the correct response.  Also, on my machine, I have the following drives, c, d, e. f, g, h and I.  Your program does not allow most of them?!?

Paul

Nilrem

Works a treat for me on all of my drives. Type (without quotation marks) "C:\" that will work.