Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[Homework] [x86] read file, add 1, write to file.

ia32 assembly

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 SHEP

SHEP

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 30 November 2014 - 04:42 PM

Hello I am working on what seemed like a simple assignment. I have a file i need to open full of integers ex. 1,10,32. I need to read the integers from the file and add 1. Then output the numbers to a different file. I believe i have most of the code however I am running into the program writing garbage the the output file and not the numbers. Please take a look at what I have and help me fix my problem.

.data
num:
	.int 10
filename:
	.asciz "indata.txt"
out:
	.asciz "outdata.txt"
.bss
	.lcomm filehandle, 4
	.lcomm outfilehandle, 4
.text
.globl _start
_start:
	nop
	# Open input file
	movl $5, %eax #open file
	movl $filename, %ebx #filename
	movl $00, %ecx #rd
	movl $0444, %edx
	int $0x80
	test %eax, %eax #Test
	js badfile
	movl %eax, filehandle
						
	# Open output file
	movl $5, %eax
	movl $out, %ebx
	movl $01101, %ecx
	movl $0644, %edx
	int $0x80
	test %eax, %eax
	js badfile
	movl %eax, outfilehandle
	
readloop:
	#Read and store input.
	movl $3, %eax #Read
	movl filehandle, %ebx	
	movl $num, %ecx #memory adress 
	movl $5, %edx #length
	int $0x80		
	test %eax, %eax #Test
	jz done
	js badfile
	
	addl $1, num

	# Write output to file
	movl $4, %eax #Write
	movl outfilehandle, %ebx # To out filehandle
	movl $num, %ecx
	movl $5, %edx

	int $0x80
	test %eax,%eax
	js badfile
	jmp readloop

badfile:
	movl %eax, %ebx
	movl $1, %eax
	int $0x80

done:

	#Close file.
	movl $6, %eax
	movl filehandle, %ebx
	int $0x80

	#Close file.
	movl $6, %eax
	movl out, %ebx
	int $0x80

	movl $1, %eax
	movl $0, %ebx
	int $0x80
	


#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 30 November 2014 - 05:57 PM

This is definitely not a trivial assignment. You're treating the integers as binary data so all you're doing is incrementing the ASCII character value and writing that. For example: Create a file containing five forward slashes ("/////" without quotes). I bet you the output file will contain either "00000" or "0////", I'm not 100% sure from your code.

 

What you need to do is read in an integer string, convert that string to an actual integer you hold in a register, increment it, then convert it back into a string and write that string out to a file.


Edited by dargueta, 30 November 2014 - 06:02 PM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 SHEP

SHEP

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 30 November 2014 - 06:17 PM

This is definitely not a trivial assignment. You're treating the integers as binary data so all you're doing is incrementing the ASCII character value and writing that. For example: Create a file containing five forward slashes ("/////" without quotes). I bet you the output file will contain either "00000" or "0////", I'm not 100% sure from your code.

 

What you need to do is read in an integer string, convert that string to an actual integer you hold in a register, increment it, then convert it back into a string and write that string out to a file.

I have tried your solution by calling atoi, my problem is I need to read line by line because I don't know how many bytes are in each line. Is there a way to read till the new line?



#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 30 November 2014 - 06:21 PM

Well, if you can use the C library, you might as well use fopen, fscanf, and fclose.


sudo rm -rf / && echo $'Sanitize your inputs!'