Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6838251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:34:27+00:00 2026-05-26T23:34:27+00:00

I am testing an assembler I am writing which generates X86 instructions. I would

  • 0

I am testing an assembler I am writing which generates X86 instructions. I would like to do something like this to test whether the instructions work or not.

#include<stdio.h>

unsigned char code[2] = {0xc9, 0xc3};

int main() {
  void (*foo)();
  foo = &code;
  foo();
  return 0;
}

However it seems that OS X is preventing this due to DEP. Is there a way to either (a) disable DEP for this program or (b) enter the bytes in another format such that I can jump to them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:34:28+00:00Added an answer on May 26, 2026 at 11:34 pm

    If you just need to test, try this instead, it’s magic…

    const unsigned char code[2] = {0xc9, 0xc3};
    ^^^^^
    

    The const keyword causes the compiler to place it in the const section (warning! this is an implementation detail!), which is in the same segment as the text section. The entire segment should be executable. It is probably more portable to do it this way:

    __attribute__((section("text"))
    const unsigned char code[2] = {0xc9, 0xc3};
    

    And you can always do it in an assembly file,

        .text
        .globl code
    code:
        .byte 0xc9
        .byte 0xc3
    

    However: If you want to change the code at runtime, you need to use mprotect. By default, there are no mappings in memory with both write and execute permissions.

    Here is an example:

    #include <stdlib.h>
    #include <sys/mman.h>
    #include <err.h>
    #include <stdint.h>
    
    int main(int argc, char *argv[])
    {
        unsigned char *p = malloc(4);
        int r;
        // This is x86_64 code
        p[0] = 0x8d;
        p[1] = 0x47;
        p[2] = 0x01;
        p[3] = 0xc3;
        // This is hackish, and in production you should do better.
        // Casting 4095 to uintptr_t is actually necessary on 64-bit.
        r = mprotect((void *) ((uintptr_t) p & ~(uintptr_t) 4095), 4096,
                     PROT_READ | PROT_WRITE | PROT_EXEC);
        if (r)
            err(1, "mprotect");
        // f(x) = x + 1
        int (*f)(int) = (int (*)(int)) p;
        return f(1);
    }
    

    The mprotect specification states that its behavior is undefined if the memory was not originally mapped with mmap, but you’re testing, not shipping, so just know that it works just fine on OS X because the OS X malloc uses mmap behind the scenes (exclusively, I think).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Testing: return request.getCookies() == null; is not an appropriate way test. Is there another
I am currently writing a unit test framework which shall in the end run
I am just testing and trying to learn how assembler works with C. So
Unit testing is, roughly speaking, testing bits of your code in isolation with test
This was driving me a bit nuts as testing strong singing and installation to
I'm writing a test WinForms / C# / .NET 3.5 application for the system
I am currently playing around with x86 assembler since I wanted to refresh my
I have a Intel assembly x86(16 bit version) assembly code as below which prints
I'm currently writing an analysis system within which, in the solution, I have created
In NUnit, I'm used to writing Trace statements in the test, and having them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.