News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Problem with macros

Started by g6g6, July 01, 2014, 06:29:42 AM

Previous topic - Next topic

g6g6

Hello. I am doind a list of exercises..And one question ask me:
The Caesar Cipher is an encryption method widely used in ancient times. she
consists of performing a kind of rotation between the letters of a given
message a given number of times. For example, the message "abc" and "xyz" to
passing a Caesar Cipher factor of 3, would become "def" and "abc", respectively.
Build a program that reads a file a message of up to 20
characters (all characters are lowercase and no spaces) and in print
another file the message applied to a Caesar Cipher factor of 13.
Caesar cipher: http://pt.wikipedia.org/wiki/Cifra_de_C% C3% A9sar
example
Input file: cifra.txt
Check-in: monitoria
Output File: decifra.txt
Released: zbavgbevn

So, this is my code:
http://pastebin.com/q33j9JU8

Please, read with attention.. This is not all the code.. I just made this code to verify if my technique will work. So,my technique consists in verify what is the character from input.txt... To do this, I will make a list of conditions ( as you can see in my code, I have used just one condition to simplify), and, if the condition is true , my program creates the output.txt with the character modified with the conditions of the exercise (ex: if the character of input.txt is "a", create a output.txt file with character "n")...So, I tried do this, but this is not working, because when I tried to compare the character of input.txt with other character , the program says that they are not equal, being that they are equal.
Could you help me?

gelatine1

Did you check the values of ebx and eax ? I believe ebx will be 97 but eax probably won't. I assume something went wrong in the reading process ?
Try this code instead:


.if eax==97
invoke crt_fwrite,offset igu,1,1,handle2
.else
invoke crt_fwrite,offset dif,1,1,handle2
.endif


also try


.if ebx==97
invoke crt_fwrite,offset igu,1,1,handle2
.else
invoke crt_fwrite,offset dif,1,1,handle2
.endif


But why are you checking if the characters are the same ? What I would do for the The Caesar Cipher is the following:

1) read file, store data in buffer
2) loop over each character in the buffer
3) perform value=((value-97 +13) % 26) +97 on each value (13 because thats the factor you gave)
    Do you understand why ?
4) write the buffer to the output file


g6g6

Yes!!! Make more sense.. and is more easy..Tks.. But why the values is not equal? If you look my code, in the comment I put 2 printfs functions to verify the value of eax and ebx, and they are equal, but when I use the macro, says that they are not equal.

gelatine1

You think this error is somehow in the definition of the Macro  :shock: I wouldn't know, I never actually use those. But you could verify by using this code:


cmp eax,ebx
jnz dif
invoke crt_fwrite,offset igu,1,1,handle2
jmp continue
different:
invoke crt_fwrite,offset dif,1,1,handle2
continue:
...


It does exactly the same but it doesn't use the macro :) So if this does work as expected then there is indeed something wrong with the macro.

qWord

First of all, please add the code to your posts instead of linking to external sides!

You must take care of the WinABI, which says that only ESI, EDI, EBX (and EBP) are preserved across API calls (sdtcall/C-Call). That means EAX, ECX and EDX are modified by the called function. The macros coming with MASM32 SDK also follow this convention. Also remarks that the INVOKE directive might use the EAX register when the ADDR operator is used.
An other problem is that you read only one byte, but then load 4 byte for compare --> MOVZX.

BTW: the unused format string "printf" is not zero terminated.
MREAL macros - when you need floating point arithmetic while assembling!

Gunther

Quote from: qWord on July 01, 2014, 07:35:55 AM
First of all, please add the code to your posts instead of linking to external sides!

Right.

Quote from: qWord on July 01, 2014, 07:35:55 AM
BTW: the unused format string "printf" is not zero terminated.

Good catch, qWord.

Gunther
You have to know the facts before you can distort them.