Question

In: Electrical Engineering

How to use or embed assembly routines/codes in C programs?

How to use or embed assembly routines/codes in C programs?

Solutions

Expert Solution

Many programmers are more comfortable writing in C, and for good reason: C is a mid-level language (in comparison to Assembly, which is a low-level language), and spares the programmers some of the details of the actual implementation.

However, there are some low-level tasks that either can be better implemented in assembly, or can only be implemented in assembly language. Also, it is frequently useful for the programmer to look at the assembly output of the C compiler, and hand-edit, or hand optimize the assembly code in ways that the compiler cannot. Assembly is also useful for time-critical or real-time processes, because unlike with high-level languages, there is no ambiguity about how the code will be compiled. The timing can be strictly controlled, which is useful for writing simple device drivers. This section will look at multiple techniques for mixing C and Assembly program development.

Inline AssemblyEdit

One of the most common methods for using assembly code fragments in a C programming project is to use a technique called inline assembly. Inline assembly is invoked in different compilers in different ways. Also, the assembly language syntax used in the inline assembly depends entirely on the assembly engine used by the C compiler. Microsoft C++, for instance, only accepts inline assembly commands in MASM syntax, while GNU GCC only accepts inline assembly in GAS syntax (also known as AT&T syntax).

#include<stdio.h>
 
void main() {
 
   int a = 3, b = 3, c;
 
   asm {
      mov ax,a
      mov bx,b
      add ax,bx
      mov c,ax
   }
 
   printf("%d", c);
}





2)

​​​​​​#include "stdio.h"
#include<iostream>

int main(void)
{
    unsigned char r1=0x90,r2=0x1A,r3=0x2C;
    unsigned short m1,m2,m3,m4;
    __asm__
    {
        MOV AL,r1;
        MOV AH,r2;
        MOV m1,AX;
        MOV BL,r1;
        ADD BL,3;
        MOV BH,r3;
        MOV m2,BX;
        INC BX;
        DEC BX:
        MOV m3,BX;
        SUB BX,AX;
        MOV m4,AX;
        
    }
    printf("r1=0x%x,r2=0x%x,r3=0x%x\n",r1,r2,r3);
    printf("m1=0x%x,m2=0x%x,m3=0x%x,m4=0x%x\n",m1,m2,m3,m4);
    return 0;
}

Linked Assembly

When an assembly source file is assembled by an assembler, and a C source file is compiled by a C compiler, those two object files can be linked together by a linker to form the final executable. The beauty of this approach is that the assembly files can be written using any syntax and assembler that the programmer is comfortable with. Also, if a change needs to be made in the assembly code, all of that code exists in a separate file, that the programmer can easily access. The only disadvanges of mixing assembly and C in this way are that a)both the assembler and the compiler need to be run, and b) those files need to be manually linked together by the programmer.


Related Solutions

Which programs (C and assembly, etc.) of xv6 has the codes for the boot procedure? Can...
Which programs (C and assembly, etc.) of xv6 has the codes for the boot procedure? Can we use printf() to print a message during the boot process and why?
Which programs (C and assembly, etc.) of xv6 has the codes for the boot procedure? Can...
Which programs (C and assembly, etc.) of xv6 has the codes for the boot procedure? Can we use printf() to print a message during the boot process and why?
C++ on randomly generated integers stored in vectors. you will use routines from STL <algorithm> to...
C++ on randomly generated integers stored in vectors. you will use routines from STL <algorithm> to implement these algorithms. Using the following functions with their description const int DATA_SIZE = 200; const int SEARCH_SIZE = 100; const int DATA_SEED = 7; const int SEARCH_SEED = 9; void genRndNums( vector<int>& v, int seed ) { This routine generates random integers in the range [ LOW = 1, HIGH =200 ] and puts them in vector v. Initializes the random number generator...
How does the use of safe and predictable routines contribute to a person’s sense of security?...
How does the use of safe and predictable routines contribute to a person’s sense of security? Give one example of when mandatory reporting is required. What should you do if something is outside your scope of knowledge, skills or work role? What is meant by discrimination? Give two examples of human rights.
In C++, use SML programs to accomplish each of the following tasks: a) Use a sentinel-controlled...
In C++, use SML programs to accomplish each of the following tasks: a) Use a sentinel-controlled loop to read positive numbers and compute and display their sum. Terminate input when a negative number is entered. b) Use a counter-controlled loop to read seven numbers, some positive and some negative, and compute and display their average. c) Read a series of numbers, and determine and display the largest number. The first number read indicates how many numbers should be processed. For...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
Download the historical stock price of your company (intel) and use them to calculate returns. Embed...
Download the historical stock price of your company (intel) and use them to calculate returns. Embed an Excel Spreadsheet showing the stock price and returns with only few lines showing. Download the historical price of the S&P 500 index to use as an approximation of market performance. Use the prices to find returns. Embed an Excel Spreadsheet showing the stock price and returns with only few lines showing. Find the slope of a regression in which returns of the company...
Identify and describe how you use technology and its effects on your daily/weekly/monthly routines. Think about...
Identify and describe how you use technology and its effects on your daily/weekly/monthly routines. Think about how technology has changed the way we live. Please respond to the question in paragraph form, grammatically correct, in complete sentences. Use 200-250 words as a guide.
Describe how HCPCS codes differ from CPT codes.?
Describe how HCPCS codes differ from CPT codes.?
Describe how to use the most current diagnostic codes and the importance of the alphabetic index.
Describe how to use the most current diagnostic codes and the importance of the alphabetic index.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT