News:

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

Lodsb Stosb

Started by Ret, April 28, 2012, 07:28:29 PM

Previous topic - Next topic

Ret

greetings..!

i have a seemingly simple issue, but my limited awareness in masm
is just not enough to resolve this satisfactorily. so i seek your help...

include c:\masm32\include\masm32rt.inc
.data
      str1  byte  "abc+def",0
      str2  byte  ?

.code
start:
      mov esi,offset str1
      mov edi,offset str2
      mov ecx,sizeof str1
     
      L1:
      lodsb
      inc eax
      stosb
      loop L1

invoke StdOut, offset str2        ; output is bcd,efg
invoke ExitProcess,0
end start

comment !
output that i get is - bcd,efg -  so +  is incremented to a char comma( , )
but i would like to get the incremented value of  +  in hex, in the output
the hex value for +  is 2b. so iam looking for a value ( 2c) in the output
iam hoping to be getting - bcd2cefg - as output from this simple code

how do i go about doing it right..?
please do let me know...thank you..!                   

dedndave

first, you want to allocate more than 1 byte for str2
str2  byte 16 dup(?)
or something similar

as for the result
you aren't quite providing enough information about the desired algorithm
i.e. - i can make it be 'bcd2cefg'   :bg
but i do not understand what the function is supposed to do for other input values

could you describe it a little better ?

Ret

Quote from: dedndave on April 28, 2012, 07:34:14 PM
first, you want to allocate more than 1 byte for str2
str2  byte 16 dup(?)
or something similar

as for the result
you aren't quite providing enough information about the desired algorithm
i.e. - i can make it be 'bcd2cefg'   :bg
but i do not understand what the function is supposed to do for other input values

could you describe it a little better ?

thank you for your  response ..!
the issue is more like this:

i like to have a password from a string say "abcd+@ dcba" by add or xor -ing it with a value and that password should
contain only decipherable characters that my malformed minute brain is capable of retaining, and reproducing when needed.

in other words, that password should contain only 0-9 and A-Z and a-z characters only.

Back to your suggestion that more memory needed to be allocated for - str2   byte  16 dup(?)  i was again on a mistaken
notion that, the  needed amount of memory would be allocated at runtime either by assembler or by os. . . how wrong i was ..

thank you once again for correcting me..!

dedndave

#3
so - is the "+" a seperator between the ID and password ?

i think what you may be interested in is some form of base conversion
the resulting string may be longer, but you are able to represent a larger number of characters

for example:
'abcd' may be represented as '61626364'
that is the hex string for 'abcd'
so - i can represent up to 256 different input characters, using only 16 different output characters

you have to start by defining the number of different characters that are allowed in the input string
you have already defined the number of characters allowable in the output string as 62

xandaz

Im not sure this is what you want....
include c:\masm32\include\masm32rt.inc
.data
      str1  byte  "abc+def",0
      str2  byte  ?

.code
start:
      mov esi,offset str1
      mov edi,offset str2
      mov ecx,sizeof str1
     
      L1:
      lodsb
      cmp al,'+'
      jne   increase
      mov ax,'2c'
      stosw
      dec  ecx
      jmp L1
increase:
      inc eax
      stosb
      loop L1

invoke StdOut, offset str2        ; output is bcd,efg
invoke ExitProcess,0
end start

... but i think its simple enough

xandaz

    mov ax,'c2' ??? i think its backwards

raymond

Quotemov ax,'c2' ??? i think its backwards

No it's not backwards. Remember that memory is little-endians. The "2" would get into the AL register and the "c" would go into the AH register. When storing "AX", the content of the AL register would be at a lower address than the content of the AH register. Printing the content of memory, the "2" would then be displayed before the "c".

mov eax,"abcd"
mov mem1,eax

Printing the content of mem1 would result in dcba.

@Ret,

What would you want displayed when converting z or Z? or 9?????
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

dedndave

... or '2C' ?????   :red

raymond

Quote from: dedndave on April 29, 2012, 03:42:02 AM
... or '2C' ?????   :red

Only if mov ax,"C2" is used. :wink
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Ret

Quote from: xandaz on April 29, 2012, 01:17:04 AM
Im not sure this is what you want....
include c:\masm32\include\masm32rt.inc
.data
      str1  byte  "abc+def",0
      str2  byte  ?

.code
.. ..     
      L1:
      lodsb
      cmp al,'+'
      jne   increase
      mov ax,'2c'
      stosw
      dec  ecx
      jmp L1
increase:
      inc eax
      stosb
      loop L1


... but i think its simple enough

thank you .. all of you for your valued suggestion to the question mark

include c:\masm32\include\masm32rt.inc
.data
      str1  byte  "abc=+=def"     
      str2  byte  ?

.code
start:
      mov esi,offset str1
      mov edi,offset str2
      mov ecx,sizeof str1
     
      l1:
      lodsb
      inc eax
      stosb
      loop l1

invoke StdOut, offset str2     ; output bcd>,>efg
invoke ExitProcess,0
end start

hi!
it's evident that i had clearly failed to get across the issue with the above code while seeking your valued help for a solution...

let me state it, this manner:

i need to generate a password from a string " abc=+=def " - the string could be of any custom made with chars like @/#/etc

back to the string abc=+=def

by incrementing the byte  : a  changesTo (b)  and  =  changesTo  (>)  and  +  changesTo  (,)
and i get an output of characters as: bcd>,>efg

this far i could reach without much difficulty..
what i need now, is:

while retaining the alphabets like a-z in both cases, i would like to replace chars like (=) and (,) with its equivalent
hex values . like the replacing of  >  with 3e and the  ,  with 2c.

so, i would have an array of characters looking like: bcd3e2c3edef as password
how do i get it done in the above program..

thank you..!

jj2007

Quote from: Ret on April 29, 2012, 07:21:51 AM
back to the string abc=+=def
...
so, i would have an array of characters looking like: bcd3e2c3edef as password

So you want to do some basic encryption, i.e. take a password, change it a little bit, store it together with normal text so that people who are not supposed to see the content cannot just open the text file in an editor.

That technique is low tech, but for the average grandma it will do fine.

The problem with bcd3e2c3edef is that the program will have difficulties recognising that some of the characters are hex codes. You could tell the receiving program that a 2 or 3 means "hexcode is following" - but then you cannot use the numbers in your password.

What you can typically use is some other transformation on a byte-per-byte basis. Below are three methods. Looks like pseudocode but it's actually assembler ;-)
The or 128 is probably the best because most text readers have no problems with the extended ASCII set (codes 128-255).

include \masm32\MasmBasic\MasmBasic.inc   ; download
   Init
   Let esi=Input$("Type a password: ", "abc=+=def")

   Print CrLf$, "Original:", Tb$, "[", esi, "]", CrLf$, CrLf$

   For_ ebx=0 To Len(esi)-1
      inc byte ptr [esi+ebx]       ; Method 1: inc
   Next
   Print "Plus one:", Tb$, "[", esi, "]", CrLf$

   For_ ebx=0 To Len(esi)-1
      dec byte ptr [esi+ebx]       ; dec to restore password
   Next
   Print "Restored:", Tb$, "[", esi, "]", CrLf$, CrLf$

   For_ ebx=0 To Len(esi)-1
      or byte ptr [esi+ebx], 128       ; Method 2: or 128
   Next
   Print "Or 128:    ", Tb$, "[", esi, "]", CrLf$

   For_ ebx=0 To Len(esi)-1
      and byte ptr [esi+ebx], 127       ; and 127 to restore password
   Next
   Print "Restored:", Tb$, "[", esi, "]", CrLf$, CrLf$

   For_ ebx=0 To Len(esi)-1
      not byte ptr [esi+ebx]       ; Method 3: not
   Next
   Print "Not:       ", Tb$, "[", esi, "]", CrLf$

   For_ ebx=0 To Len(esi)-1
      not byte ptr [esi+ebx]       ; Method 3: xor
   Next
   Inkey "Restored:", Tb$, "[", esi, "]", CrLf$
   Exit
end start

I attach the executable so that you can test the outcomes of different passwords. Your example yields the following:
Type a password: abc=+=def

Original:       [abc=+=def]

Plus one:       [bcd>,>efg]
Restored:       [abc=+=def]

Or 128:         [áâ㽫½äåæ]
Restored:       [abc=+=def]

Not:            [ž?œÂÔ›š™]
Restored:       [abc=+=def]

dedndave

#11
well - what they do in URL's is use a '%' character
encoded:
abcdef%2fghijkl
decoded:
abcdef/ghijkl

the parser sees the '%' and it knows the next 2 characters are a hexidecimal representation of a single character
if you want a literal '%', you use '%%' or '%25'

jj2007

Instead of using the %, you might as well hex-encode the whole password

   xchg Len(esi), ecx       ; Method 4: hex
   .While ecx
      dec ecx
      movzx eax, byte ptr [esi+ecx]
      movzx edx, word ptr [Cat$(Right$(Hex$(eax), 2))]
      mov [esi+2*ecx], dx
   .Endw
   Print "Hex:       ", Tb$, "[", esi, "]", CrLf$
   sar Len(esi), 1
   push eax
   xor ecx, ecx
   .Repeat
      lea ebx, [2*ecx+1]
      mov eax, Val(Cat$(Mid$(esi, ebx, 2)+"h"))
      mov [esi+ecx], al
      inc ecx
   .Until ecx>=[esp]
   mov byte ptr [esi+ecx], ah
   pop eax
   Inkey "Restored:", Tb$, "[", esi, "]", CrLf$

Type a password: abc=+=def

Hex:            [6162633D2B3D646566]
Restored:       [abc=+=def]

dedndave

well - you could "mangle" the characters A-Z, a-z, 0-9 (Drogan style or whatever)
the advantage is shorter strings   :P