3 Replies - 1264 Views - Last Post: 05 July 2012 - 12:21 PM

#1 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 384
  • Joined: 18-September 11

Help reading from a file

Posted 04 July 2012 - 08:37 PM

I've got this much:

filename:
    .asciz  "22names.txt"

dec32_format:
	.string "%d\n"

.section .text
.globl main
 
main:
	pushl	$filename
	call	read_file

#------------------------------------------------------------------------------
.type read_file, @ function

read_file:
        xor  %eax, %eax
        xor  %ebx, %ebx
        xor  %ecx, %ecx
        xor  %edx, %edx

        mov  $0x05, %al     # 5 sys_open(const char *, int, int)
        mov  4(%esp), %ebx  # load string pointer into register
        xor  %ecx, %ecx     # O_RDONLY (open read only)
        int  $0x80
 
        mov  %eax, %esi    # file descriptor
        jmp  read
 
read:
        mov  $0x03, %al    # 3 sys_read(unsigned int, char *, size_t)
        mov  %esi, %ebx    # file descriptor (unsigned int)
        lea  (%esp), %ecx  # 
        mov  $0x01, %dl    # read only 1 char (third arg)
        int  $0x80         # sys_call
 
        xor  %ebx, %ebx    # clear out ebx
        cmp  %eax, %ebx    # compare eax and ebx
        je   exit          # if EAX && EBX == NULL, EOF
 
        mov  $0x04, %al    # 4 sys_write(unsigned int, const char *, size_t)
        mov  $0x01, %bl    # write to STDOUT
        mov  $0x01, %dl    # write one character
        int  $0x80         # call it
 
        jmp  read          # read the next char

exit: 
        xor  %ebx, %ebx
        mov  $0x01, %al
        int  $0x80




The code works perfectly fine in that it reads the file and writes its contents on the console. But that's not what I need. I need to store the current character in a register so I can use it to do some computations. But this code doesn't seem to be storing the character in any register. I'm using this code as a template (I got it from a website) but I'm having no luck modifying it to do what I need. So can you please tell me how I need to change it?


Thanks!

This post has been edited by carnivroar: 04 July 2012 - 08:39 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help reading from a file

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 876
  • View blog
  • Posts: 2,312
  • Joined: 28-March 11

Re: Help reading from a file

Posted 04 July 2012 - 08:49 PM

almost 12, need sleep so I don't have the time to fire up Linux, but you read a character, BUT that character is NOT in EAX but AL (the byte register of eax), right is that what register sys_read returns the character - eax?

instead of this:
   xor  %ebx, %ebx    # clear out ebx
   cmp  %eax, %ebx    # compare eax and ebx
   je   exit 

just do:
    test %eax, %eax
    jz   exit

Was This Post Helpful? 1
  • +
  • -

#3 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 384
  • Joined: 18-September 11

Re: Help reading from a file

Posted 05 July 2012 - 07:41 AM

I'm not sure what the code is doing, I got it from here.

http://r00tsecurity....er-inefficient/

I just need to know how to store the current character in a register so I could use it.

I also looked at 2 books I have but it wasn't very helpful either. I'm really stuck now.

This post has been edited by carnivroar: 05 July 2012 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#4 carnivroar  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 384
  • Joined: 18-September 11

Re: Help reading from a file

Posted 05 July 2012 - 12:21 PM

Found it:

http://bluemaster.iu...m/syscalls.html

After the system call, the pointer to the character is stored in ecx. So I have to use movzx (%ecx), dest to get my char into the destination.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1