Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

In-line assembly in c in keil

Status
Not open for further replies.

embpic

Advanced Member level 3
Joined
May 29, 2013
Messages
742
Helped
80
Reputation
160
Reaction score
77
Trophy points
1,308
Location
india
Activity points
5,213
2.png3.pngUntitled.pngHello sir's
i have tried assembly language and C for p89v51rd2 using KEIL compiler but if try In-line assembly in c then it give lots of warning and error also .

even if i tik follow i am getting error as follows.
this is simple example i tried.
thanx
 
Last edited:

Apparently the assembler doesn't understand your syntax. What do you mean to say with this line?

Code:
jmp $
 

actually i add this just for example.
but when i use instrn as

mov a,#55

then also it gives error befor adding any assembly it works fine 0 error and 0 wanrnings

but if i add assembly it gives error regarding startup.s like that means i don't know which file to include or exclude so guide me.

thanx
 

Did you find anything about __asm in the C51 users manual? I think, Keil C51 uses a #pragma asm instead.
 
As FvM indicated, use of the directives #pragma asm and #pragma endasm are required when using the C51 Compiler.

Reference: C51: GETTING INLINE ASSEMBLY TO WORK

The following example program, MAIN.C, demonstrates some simple inline assembly.

Code:
void main(void){
   test();

#pragma asm
      JMP   $           ; endless loop
#pragma endasm
}

The following rules apply to C51 inline assembly:

To use #pragma ASM/ENDASM, you must set the Generate Assembler SRC File and Assemble SRC File source file compile options in µVision by right-clicking your source file name in the Project Workspace and selecting Options for File.
When using the Generate Assembler SRC File and Assemble SRC File options in µVision, if there are no other C modules in your project, you must manually include the C51 Run-Time Library (C51S.LIB, C51C.LIB, or C51L.LIB).



BigDog
 
Last edited:

if i declare any variable in c data type then is it accessible from assembly.
and procedure is correct or not?
and give me procedure also with example.
thanx.
 

if i declare any variable in c data type then is it accessible from assembly.
and procedure is correct or not?
and give me procedure also with example.

While accessing a variable define in C code within an inline assembly can be accomplished, a more prudent method would be to utilize an assembly routine which is then called by the main C program.

A possible shortcut would be to use the #PRAGMA SRC directive with a dummy C routine to generate assembly code, which could then be modified as needed, assembled and then called by the original C program.

Reference: **broken link removed** C51: CALLING ASSEMBLY ROUTINES FROM C"]C51: CALLING ASSEMBLY ROUTINES FROM C

There are no examples in the book, but it is easy to create your own. The asm routine must know how parameters are passed, values returned, and the naming conventions of segments. The steps you must follow to create an example are outlined below:

1. Write a simple function in C that passes parameters and returns values the way you want your assembly routine to.

2. Use the SRC directive (#PRAGMA SRC at the top of the file) so that the C compiler generates a .SRC file instead of a .OBJ file.

3. Compile the C file. Since the SRC directive was specified, the .SRC file is generated. The .SRC file contains the assembly code generated for the C code you wrote.

4. Rename the .SRC file to a .A51 file.

5. Edit the .A51 file and insert the assembly code you want to execute into the body of the assembly function shell included in the .A51 file.

For example, the following code

Code:
#pragma SRC
unsigned char my_assembly_func (
  unsigned int argument)
{
return (argument + 1);   // Insert dummy lines to access all args and retvals
}

when compiled generates the following assembly SRC file.

Code:
NAME    TESTCODE

?PR?_my_assembly_func?TESTCODE                SEGMENT CODE
        PUBLIC  _my_assembly_func
; #pragma SRC
; unsigned char my_assembly_func (

        RSEG  ?PR?_my_assembly_func?TESTCODE
        USING   0
_my_assembly_func:
;---- Variable 'argument?040' assigned to Register 'R6/R7' ----
                        ; SOURCE LINE # 2
;   unsigned int argument)
; {
                        ; SOURCE LINE # 4
; return (argument + 1);   // Insert dummy lines to access all args and retvals
                        ; SOURCE LINE # 5
        MOV     A,R7
        INC     A
        MOV     R7,A
; }
                        ; SOURCE LINE # 6
?C0001:
        RET
; END OF _my_assembly_func

        END


BigDog
 
Last edited by a moderator:

as you given procedure i follow it but getting confused. my source code is as follows

Code:
#pragma SRC
#include<reg51f.h>
#include<intrins.h>

unsigned int delay(unsigned int);

void main()
{
	while(1)
	{
		P0 =~P0;
		delay(100);
	}
}

unsigned int delay(unsigned int del)
{
//	mov a,#44
return (del+1);
}
//		assembly line i want to insert
//back: djnz del,back

and after i compiled it generate main.src so i renamed as main.A51 so portion of file as follows

Code:
unsigned int delay(unsigned int del)

	RSEG  ?PR?_delay?MAIN
_delay:
	USING	0
			; SOURCE LINE # 16
;---- Variable 'del?140' assigned to Register 'R6/R7' ----
; {
//********************source code1

			; SOURCE LINE # 17
; return (del+1);
//********************source code2
			; SOURCE LINE # 18
	MOV  	A,R7
	ADD  	A,#01H
	MOV  	R7,A
	CLR  	A
	ADDC 	A,R6
	MOV  	R6,A
; }			; SOURCE LINE # 19
?C0004:
	RET  	
; END OF _delay

	END

Ok this is my main.src file. So, where to write assembly code at source code1 or at source code2.

wheather to include main.A51 file to Target or not?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top