To be upfront and all, this is indeed a homework assignment. But I'm not looking for anyone to write the code I need just help me with a few functions I'm needing to use (the book is VERY vague on how to use these instructions).

Assignment is open an encrypted file, decrypt it and then send it to a new text file.

Ok so far I have this

.data
BUFFER_SIZE = 5000
buffer BYTE BUFFER_SIZE DUP(?)
filename BYTE "myfile.txt",0 (this is coming from my book)

.code
mov edx,OFFSET filename
call OpenInputFile
mov edx,OFFSET buffer
mov ecx,BUFFER_SIZE
call ReadFromFile
call WriteToFile

As I said the book is extremly vague on how these instructions truly work. My question is what is the normal procedure to open a file, read it and then output it to a new file?

Recommended Answers

All 7 Replies

Those functions could be in some library that I don't have, but perhaps writing those functions is the REAL assignment.

Could you give us some idea what assembler you use, on what operating system, etc? Maybe even the name of the course and book would give us a hint on helping you.

So far, your post doesn't give some of us enough info, unless those functions happen to belong to a package that someone else recognizes.

Those functions could be in some library that I don't have, but perhaps writing those functions is the REAL assignment.

Could you give us some idea what assembler you use, on what operating system, etc? Maybe even the name of the course and book would give us a hint on helping you.

So far, your post doesn't give some of us enough info, unless those functions happen to belong to a package that someone else recognizes.

I'm using MASM. The book is Assembly Language for Intel-Based Computers 5th Edition.

The functions are built into the irvine32.lib, so writing the functions isn't part of the assignment.

Goto http://cis.ysu.edu/~rick/class3714/ch5.html and search for OpenInputFile and you will find the three functions documented near one another.

I see that you need to use EAX after that first call, the rest should follow naturally from the info at that URL. Give it a go, and let us know how it goes.

I have seen a lot of motion in the direction of making assembler behave similarly to other languages. Hard to say, but that irvine package might even ship with something similar to function prototypes to point you in the right direction. I have some masm stuff like that.

Goto http://cis.ysu.edu/~rick/class3714/ch5.html and search for OpenInputFile and you will find the three functions documented near one another.

I see that you need to use EAX after that first call, the rest should follow naturally from the info at that URL. Give it a go, and let us know how it goes.

I have seen a lot of motion in the direction of making assembler behave similarly to other languages. Hard to say, but that irvine package might even ship with something similar to function prototypes to point you in the right direction. I have some masm stuff like that.

Dan,

Thanks for that link it really helped. I was able to read in a file and output it to a new one. Now I'm needing a little help with the 2nd half of the assignment.

We were given an encrypted file and we need to read it in and decrypt it using XOR (and the HEX decode number).

What's happening is it exports to a file, but it's a mixture of useless words and still some special characters. It's suppose to be a question for us to answer. Can you look at my code and see where I'm making my mistake?

INCLUDE Irvine32.inc
.data
BUFFER_SIZE = 5000
buffer BYTE BUFFER_SIZE DUP(?)
robert BYTE "inputfile.txt",0
filenam BYTE "testing.txt",0
fileHandle DWORD ?
bytesRead DWORD ?
a1 DWORD ?

.code
main PROC
    
     ;--------------------------------------------------------;
     ; Open File								       ;
     ;--------------------------------------------------------;
	mov edx,OFFSET robert
	call OpenInputFile
	mov fileHandle,eax
	
	;--------------------------------------------------------;
	; Read file content into buffer                          ;
	;--------------------------------------------------------;
	mov eax,fileHandle
	mov edx,OFFSET buffer
	mov ecx,BUFFER_SIZE
	call ReadFromFile
	mov bytesRead,eax
	mov eax,bytesRead
	
	;-------------------------------------------------------;
	; Copy content of buffer and decrypt                    ;
	;-------------------------------------------------------;
	mov a1,edx
	xor a1,0ffh

     ;--------------------------------------------------------;
     ; Create output file and write buffer to file            ;
     ;--------------------------------------------------------;
	mov edx, OFFSET filenam
	call CreateOutputFile
	mov fileHandle,eax
	mov eax,fileHandle
	mov edx,OFFSET buffer
	mov edx, a1 ; Copy decryted information into EDX
	mov ecx,BUFFER_SIZE
	call WriteToFile
	mov bytesRead,eax


	call CloseFile ; Close file
	
	exit
main ENDP

END main

Thanks.

A few items:

You XOR a DWORD with 0ffh, but you need to XOR each byte in the buffer, not some randomly chosen DWORD in memory.

So, point something at your buffer and XOR byte-wise for the length of the bufffer.

I see a general misunderstanding of the "write" function call when you put the offset to the buffer in edx, then immediately put your a1 variable into edx. Now the function could point at just about anything and start printing garbage.

Another thing. If your file is over 5000 bytes long, your program is inadequate. It should sit in a loop, reading and writing until it reaches the end-of-file (EOF).

A few items:

You XOR a DWORD with 0ffh, but you need to XOR each byte in the buffer, not some randomly chosen DWORD in memory.

So, point something at your buffer and XOR byte-wise for the length of the bufffer.

I see a general misunderstanding of the "write" function call when you put the offset to the buffer in edx, then immediately put your a1 variable into edx. Now the function could point at just about anything and start printing garbage.

Another thing. If your file is over 5000 bytes long, your program is inadequate. It should sit in a loop, reading and writing until it reaches the end-of-file (EOF).

Ok I'm tired of waiting for the professor to get back to me, so can you give me an example of how to use the XOR in the context I need? Not asking for you to give me the code to solve my assignment, but something that would give me a better understanding of how to use XOR for this purpose. Honestly the book only gives about 2 lines to discuss how it works but no useful examples on how to use it.

I don't have an assembler on this box, so I wrote this in the old MS-DOS debug command. That's why it looks a little funny, isn't optimal, whatever, but hey, we're just making the best of a bad situation.

I did this by word-sized chunks, not bytes.

0B0E:0100 B90300        MOV     CX,0003
0B0E:0103 BB0002        MOV     BX,0200
0B0E:0106 8B07          MOV     AX,[BX]
0B0E:0108 35FFFF        XOR     AX,FFFF
0B0E:010B 8907          MOV     [BX],AX
0B0E:010D 43            INC     BX
0B0E:010E 43            INC     BX
0B0E:010F 49            DEC     CX
0B0E:0110 75F4          JNZ     0106
0B0E:0112 C3            RET

The data at start is the first six characters starting at 200h:
0B0E:0200 9E 9D 9C 9B 9A 99 <= unprintable chars.
After running:
0B0E:0200 61 62 63 64 65 66 <= abcdef

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.