How to assign an ASCII character via inline assembly code

This is a discussion on How to assign an ASCII character via inline assembly code within the C Programming forums, part of the General Programming Boards category; I am having difficulties executing a short __asm__() exercise: Write assembly code to store char 'A' into bufone[0]. I would ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    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,831
    I think you want "%c" to print out the ASCII character.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    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, 07: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, 11:06 AM
  5. ASCII code for a NULL character
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 12-09-2001, 04:40 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21