|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
This is a fairly common misunderstanding. The problem is that the declaration
Code:
char path[] = "file.txt" The declaration Code:
char path[]; You'll need to either declare the array size explicitly: Code:
char path[PATH_LEN]; Code:
char* path; /* ... do other things */ path = calloc(p_len, sizeof(char)); /* ...use 'path' */ free(path);
__________________
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 Short • Understanding the C/C++ Preprocessor Taming Python • A 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. |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Assembly problem - opening file with path in a string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|