Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old December 2nd, 2007, 02:51 PM
tsv tsv is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 9 tsv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 24 sec
Reputation Power: 0
Assembly problem - opening file with path in a string

I'm learning x86 assembly and am writing a program to take a file, encrypt it and send it via a comm port to another computer for a school project. It's written inline in C, due to the requirements of the assignent. Don't worry, I'm not asking anyone to do my work for me, I've added a "feature" to ask the user what file to send.

Without this feature, the program runs fine, and the path is declared and initialised as
Code:
char path[] = "file.txt"
.

When I change this and don't initialise the variable but take user input, it stops working and I have no idea why. I can print the variable, and it's contents seem to be right.

Here's my code, all help appreciated, thank you!

Code:
#include <stdio.h>
#define CR 0AH // carriage return

int main(void) {
  
  char prompt[] = "Enter path of file to send:";
  char path[300];
  char comms_name[] = "bye.txt";
  char char_to_send;
  char read_only[] = "r";
  char read_write[] = "w+";
  FILE *file;
  FILE *comms;

  __asm {
    /* ask for filepath */
      lea eax,prompt                  // move address of string to eax
      push eax                        // push address onto stack
      call printf                     // call os routine to print prompt
      add esp,4                       // scrub parameter from stack
      
      lea ebx,path                    // set pointer to path string
getnx:push ebx                        // save pointer on stack
      call getchar                    // read from STDIN
      pop ebx                         // restore pointer
      mov [ebx],al                    // save char to array
      inc ebx                         // increment pointer
      cmp al,CR                       // test for end of input
      jnz getnx                       // continue reading input
      mov [ebx],0                     // insert terminating sentinel

    /* open comm port */
      lea eax,read_write              // tell fopen we want write permission
      push eax                        // push to stack
      lea eax,comms_name              // give fopen handle
      push eax                        // push to stack
      call fopen                      // call os routine
      add esp,8                       // scrub stack
      cmp eax,0                       // check port opened successfully
      mov comms,eax                   // move data into variable
      je TERMINATION                  // terminate if port failed to open


    /* open file */
      lea eax,read_only               // tell fopen we don't need to write
      push eax                        // push to stack
      lea eax,path                    // give fopen handle
      push eax                        // push to stack
      call fopen                      // call os routine
      add esp,8                       // scrub stack
      cmp eax,0                       // check file opened successfully
      mov file,eax                    // move data into variable
      je TERMINATION                  // terminate if file failed to open

    /* read file */
WHILENOTEOF:
      mov eax,file                    // tell fgetc file to read from
      push eax                        // push onto stack
      call fgetc                      // call os routine
      add esp,4                       // scrub stack
      mov char_to_send,al             // lower 8 bits only
      cmp al,EOF                      // check for EOF
      je FILECLOSE                    // close file if end reached
      mov char_to_send,al             // get char ready to send

    /* encrypt file */


    /* send file */
      mov eax,comms                   // we are writing to comm port
      push eax                        // push to stack
      xor eax,eax                     // ???
      mov al,char_to_send             // move char ready to send
      push eax                        // push to stack
      call fputc                      // call os routine
      add esp,8                       // scrub stack
      jmp WHILENOTEOF                 // loop if more to read

FILECLOSE:
      mov eax,file                    // tell fclose what to close
      push eax                        // push to stack
      call fclose                     // call os routine



TERMINATION:
  }

  printf ("%s", path);

  return 0;
}


As you can see, it's temporarily sending to the file "bye.txt" for testing purposes.

Reply With Quote
  #2  
Old December 2nd, 2007, 09:44 PM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is online now
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,083 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 10 h 47 m 7 sec
Reputation Power: 446
This is a fairly common misunderstanding. The problem is that the declaration
Code:
   char path[] = "file.txt"
does not declare an array of char, but rather declares a pointer to a zero-delimited char array 9 bytes long. This means that the pointer path is pointing to a valid location (interestingly, I would have expected that the array would be in .rodata, in which case I wouldn't expect the code to work either way, but apparently my understanding of the matter is off).

The declaration
Code:
   char path[];
does not initialize the pointer; thus, it's not pointing to anything valid, yet. When you try to access that memory, it's pointing to somewhere which you don't have access to, hence the error.

You'll need to either declare the array size explicitly:
Code:
   char path[PATH_LEN];
(where the PATH_LEN is #defined elsewhere), or allocate memory for the pointer to point to:
Code:
   char* path;
   /* ... do other things */
   path = calloc(p_len, sizeof(char));
   /* ...use 'path'  */ 
   free(path);
(where p_len is a size determined at run time.)
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : December 2nd, 2007 at 09:58 PM.

Reply With Quote
  #3  
Old December 3rd, 2007, 02:06 AM
tsv tsv is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 9 tsv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 24 sec
Reputation Power: 0
This is what I've done, is it not? Line 7 of my above code reads

Code:
char path[300];


Or am I misunderstanding you?

Reply With Quote
  #4  
Old December 3rd, 2007, 04:42 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is online now
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,083 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 10 h 47 m 7 sec
Reputation Power: 446
Yes, that is what I mean. I must have misunderstood you, and not looked carefully enough at the code posted; I think I must have expected it to be the initialized version, and misread it from the initialization of the line below it. Sorry.

I have been making too many mistakes of this sort lately, and I'm getting concerned about it. It's there plain as day yet somehow I must have jumped into it with the wrong expectations, not paying enough attention to what is actually on the screen. Perhaps I need some time off from here, to get my bearings again, as I've been slacking on other things I need to be focusing on too much in favor of posting here.

Reply With Quote
  #5  
Old December 3rd, 2007, 01:59 PM
tsv tsv is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2007
Posts: 9 tsv User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 8 m 24 sec
Reputation Power: 0
It's fine! Thank you for trying to help! Just chill if you need to :-)

Anyone else have any suggestions? This is really annoying me as I haven't the foggiest why it's not working!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Assembly problem - opening file with path in a string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway