Thread: How to assign an ASCII character via inline assembly code

  1. #1
    Registered User
    Join Date
    May 2010
    Location
    San Jose, California, United States
    Posts
    22

    Question How to assign an ASCII character via inline assembly code

    I am having difficulties executing a short __asm__() exercise:
    Write assembly code to store char 'A' into bufone[0].

    I would think it should look like this:
    Code:
    #include <stdio.h>
    
    char* bufone;
    
    int main(int argc, char* argv[])
    {
    	__asm__("movl $0x41, bufone\n");
    	printf("\n%s\n", bufone[0]);
    	return 0;
    }
    However, I keep getting Segmentation Fault errors. What am I missing?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    bufone is not initialized pointer... you cannot use it

    bufone[0] is of type char - you cannot print it with %s

    I think before using inline asm - you need to write simple program in just C which works
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by LanguidLegend View Post
    I am having difficulties executing a short __asm__() exercise:
    Write assembly code to store char 'A' into bufone[0].

    I would think it should look like this:
    Code:
    #include <stdio.h>
    
    char* bufone;
    
    int main(int argc, char* argv[])
    {
    	__asm__("movl $0x41, bufone\n");
    	printf("\n%s\n", bufone[0]);
    	return 0;
    }
    However, I keep getting Segmentation Fault errors. What am I missing?
    Have you allocated storage for bufone?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    San Jose, California, United States
    Posts
    22
    Code:
    #include <stdio.h>
    
    char bufone[2] = {};
    
    int main(int argc, char* argv[])
    {
    		__asm__("\
    				movl $'A', bufone\n\
    	");
    	
    	printf("\n%s\n", bufone[0]);
    	return 0;
    }
    gives me this message:
    buffOF1.c: In function ‘main’:
    buffOF1.c:11: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

    But if I change
    Code:
    printf("\n%s\n", bufone[0]);
    to
    Code:
    printf("\n%x\n", bufone[0]);
    I get 41.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I think you want "%c" to print out the ASCII character.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Code:
    printf("\n%s\n", bufone[0]);
    The %s means this would try to print a string (which is really a null-terminated array of characters). bufone[0] is not (cannot be) a string. It's a single element of an array.

    Code:
    printf("\n%x\n", bufone[0]);
    The %x means it prints the value, in hex, of bufone[0]. bufone[0] holds the character 'A' which is 0x41 or 65d in ASCII.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inline assembly code check
    By todd14 in forum C++ Programming
    Replies: 7
    Last Post: 09-13-2007, 03:14 PM
  2. Replies: 3
    Last Post: 01-25-2006, 08:04 PM
  3. covert ascii code to character
    By Shadow in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 09:53 PM
  4. How to return the ASCII code value of a character in C?
    By DramaKing in forum C Programming
    Replies: 3
    Last Post: 02-11-2002, 12:06 PM
  5. ASCII code for a NULL character
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 12-09-2001, 05:40 AM

Tags for this Thread