ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have been googling the entire lesson and for some reason everything I try fails.
I have a program that uses a memory address. I used to read this from the stack pointer but now I want to be able to type the address from the command line, as an argument to the program.
Now, this address is stored in a long, but how can I convert from the string argument to a long? Here goes the program in some pseudo code:
Code:
unsigned long getsp() {
__asm__("movl %esp,%eax");
}
int main(int argc, char *argv[]) {
/* used this before, it shows the address of esp */
long sp = getsp();
printf("sp: 0x%x (%f)\n", sp, sp);
/* want to read from argument sent to program */
long new = convert_from_hex_to_long(argv[1]);
same printf as before;
}
Originally posted by jim mcnamara use strtol() to convert a string from hex to a long
Already tried, here is why it fails:
An address is 8 'characters' long, the problem is that it works with up tp 7, then something strange happens:
Code:
$ cat convert.c
#include <stdlib.h>
#include <stdio.h>
unsigned long getsp() {
__asm__("movl %esp,%eax");
}
int main(int argc, char *argv[]) {
/* used this before, it shows the address of esp */
unsigned long sp = getsp();
printf("sp: 0x%x (%d)\n", sp, sp);
/* want to read from argument sent to program */
unsigned long test = strtol(argv[1], 0, 16);
printf("test: 0x%x (%d)\n", test, test);
}
$ ./convert b
sp: 0xbffff568 (-1073744536)
test: 0xb (11)
$ ./convert bf
sp: 0xbffff568 (-1073744536)
test: 0xbf (191)
$ ./convert bfff
sp: 0xbffff568 (-1073744536)
test: 0xbfff (49151)
$ ./convert bffff55
sp: 0xbffff568 (-1073744536)
test: 0xbffff55 (201326421)
$ ./convert bffff558
sp: 0xbffff558 (-1073744552)
test: 0x7fffffff (2147483647)
With the last one the long flips over, but it's decimal value is 16 less then the value of sp, so why can sp be stored but not the address I typed in?
Any ideas?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.