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.

Keil and asm instructions ?

Status
Not open for further replies.

SphinX

Advanced Member level 3
Joined
Jan 25, 2002
Messages
822
Helped
58
Reputation
116
Reaction score
29
Trophy points
1,308
Location
EGYPT
Activity points
7,045
keil inline assembly

Hi,

How can i insert asm instructions inside the keil code ?

for example
I want to make simple function STORE

void STORE(char);

this function do the next
mov a,#b1
mov @R0,a

which i pass b1 value to this function

Thanks
 

keil inline assembler

You can use assembly subroutine.

1. Create an assembly file "ASM_FUNC.A51"
NAME ASM_FUNCTIONS
; void store(char b1)
PUBLIC store
_ASMCOD SEGMENT CODE
RSEG _ASMCOD
;-----------------------------------------
;---- Variable 'b1' assigned to Register 'R7' ----
store: MOV A,R7
MOV @R0,A
RET
;-----------------------------------------
END

2. using in C file

extern void store(char b1);

void main(void) {
...
store(0xAA);
...
}

The problem is, How can you control value of R0?
Hope this may help.
 

inline assembly in keil

Hi,

Thanks.
But how the compiler know that R7 is assignment to b1 automatically ? i misunderstand.

Pharaoh Of Egypt
 

#pragma asm

This is describe is KE*IL Compiler manual.

Try compile with C function, then see assembly output.

void TEST(char B)
{
B=0;
}

void main(void)
{
TEST(0xAA);
}

Passing Parameters in Registers
The Cx51 compiler allows up to three function arguments to be passed in CPU
registers. This mechanism significantly improves system performance as
arguments do not have to be written to and read from memory. Argument or
parameter passing can be controlled by the REGPARMS and NOREGPARMS
control directives defined in the previous chapter.
The following table details the registers used for different argument positions
and data types.
Argument Number char, 1-byte ptr int, 2-byte ptr long, float generic ptr
1..................................R7................R6 & R7......R4—R7.......R1—R3
2..................................R5................R4 & R5......R4—R7.......R1—R3
3..................................R3................R2 & R3.......................R1—R3
If no registers are available for argument passing, fixed memory locations are
used for function parameters.
 

keil c inline assembly

Pharaoh of Egypt said:
Hi,

How can i insert asm instructions inside the ke*il code ?

for example
I want to make simple function STORE

void STORE(char);

this function do the next
mov a,#b1
mov @R0,a

which i pass b1 value to this function

Another way to implement by its "Inline Assembly" feature:
Code:
void STORE( char b1 ) 
{
#pragma ASM
   MOV A,#b1
   MOV @R0,A
#pragma ENDASM
}
 

#pragma asm 8051

Hi,

Thanks ZeRoN & yager
I think that use of inline assembler is easy.

Please yager i write the next source

#include <REG51.h>
void store(char b1);
void main(void) {
store(0xaa);
}
void store( char b1 )
{
#pragma ASM
MOV A,#b1
MOV @R0,A
#pragma ENDASM
}

but when i compile it i get the next errors

MAIN.C(11): error C272: 'asm/endasm' requires src-control to be active
MAIN.C(14): error C272: 'asm/endasm' requires src-control to be active

What is the problem ?

Thanx
 

#pragma asm keil

Right click on the filename in the file manager,
then options for...
and check "Generate assembler SRC file".

The error will no longer apear.
But I don't think this method of inserting
mnemonics will work.
 

keil c272

If you need access to some memory address use macros from 'absacc.h' in include directory.

for example

#include <absacc.h>

char ch = DBYTE[30];

will read data memory at offset 30.

Tom
 

how to store in memory keil

Pharaoh of Egypt said:
MAIN.C(11): error C272: 'asm/endasm' requires src-control to be active
MAIN.C(14): error C272: 'asm/endasm' requires src-control to be active

What is the problem ?

Since the Keil could not generate OBJ directly if you use the inline assembly.
It is needed to translate into assembly source then assemble.
As the jgorsk said, you could set the options (generate/assemble SRC) in the project management.
As the ZeRoN said, you have to read manual for full take control the tool.

jgorsk said:
But I don't think this method of inserting
mnemonics will work
Yes, it could not work, but it could be fixed as these:
1. as the ZeRoN pointed, the parameter will be put into R7, so use "MOV A,R7" to substitute for "MOV A,#b1"

2. to use global variable to substitute for pass the parameter:
Code:
char b1;
void store( void ) 
{ 
#pragma ASM 
   MOV A,b1
   MOV @R0,A 
#pragma ENDASM 
} 
#define STORE(x) b1=x; store();
void main( void ) 
{
   STORE(0xaa);
}
3. to put 'b1' into 'ACC' in C:
Code:
sfr ACC = 0xE0;
store( char b1 )
{
   ACC = b1;
#pragma ASM
   MOV @R0,A
#pragma ENDASM   
}
void main( void )
{
   store(0xaa);
}
4. as the tom324 pointed, you could handle all in C

Anyway, i just point another way to implement by inline assembly, and i think you could make it by yourself.
But the jgorsk pointed out it could not work, so i made these for more exact implementation.
Though the Keil has some bugs, its inline assembly is not strong, but works.

yager
 

pragma asm

Thanks

but both of the codes (2&3) generate

Build target 'Target 1'
compiling main.c ...
Target has no object modules
Target not created


The program don't execute !

What about that please ?

Pharaoh Of Egypt
 

extern char end asm

I will try to explain the how to C call Asm principle:

C can call some function written in 8051-asm language:

The principles is:


C-language:
To call the asm-function name, must be defined as external modul.
for example to call read_ppi -> read ppi-data in address
in C-language must be define a prototype as external, like this:

extern char read_ppi(unsigned int a);

this external-function have a passing parameter in unsigned int for assigned a address of ppi that will be read. And have a return value as char as data of ppi.

for call int in main

extern char read_ppi (unsigned int addr);

main ()
{ // this is as char data in idata

char idata ppi_data;
unsigned int ppi_address;

ppi_data = read_ppi (0x2000); // this address with absolute method

ppi_address = 0x4000;
ppi_data = read_ppi (ppi_address);

}


in ASM

Must be created the function name with PUBLIC pseudo opcode, this method to tell compiler as public function


PUBLIC _read_ppi

CODE_ASM SEGMENT CODE

RSEG CODE_SEG ; for relocate code

; begin function read_ppi
USING 0 ; to tell compiler use register bank 0

_read_ppi:
// precede _ must be added to tell compiler this function have return value
// high address in R6, low address in R7 as ppi address

MOV DPH,R6
MOV DPL,R7
MOVX A,@DPTR

// to passing a return value for char must be in register R7, read manual
MOV R7,A
RET

END


Please C-source and asm source split into separate file and compile it.

Thanks'
 

error c272 asm/endasm

Pharaoh of Egypt said:
but both of the codes (2&3) generate

Build target 'Target 1'
compiling main.c ...
Target has no object modules
Target not created

The program don't execute !

What about that please ?

It just translates main.c, you also need to set 'Assemble SRC' option.
yager said:
As the jgorsk said, you could set the options (generate/assemble SRC) in the project management
To read the manual and/or check these project files.
 

name asm keil

Hi,

If u r using the "small" memory model, you will need to copy the file "c51s.lib" from the Lib folder to your project folder, or i think your project will not compile completely.

Yours,
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top