I have defined some values, see below, and I can`t use them properly.
#define add 000001
#define sub 000010
#define jmp 000111
#define IMM 10000
#define ADDR 10001
In my code, I set an address in hex.
parameter1 = false;
parameter1 = false;
uint64_t data = 0xffffffff05001e00;
uint16_t vector[4];
memcpy(vector, &data, sizeof(uint64_t));
int currentPosition = 0;
while (currentPosition < 4) {
header = vector[currentPosition];//header
opcode = header >> 0xA & 0x3F;
src1 = header >> 0x5 & 0x1F;
src2 = header & 0x1F;
if (src1 == ADDR || src1 == IMM) { parameter1 = true; }
if (src2 == ADDR || src2 == IMM) { parameter2 = true; }
....
currentPosition++;
}
header = 1e00 in this case ( because it`s vector[0] )
Forward it will do:
opcode = 0x7, src1 =0x10, src2= 0x0.
That means in binary: 000111 10000 00000 -> jmp IMM NULL
When first if is called, parameter1 should get the value true; but this never happend. Why is that happening?
I have not defined correctly IMM value?
Thanks!!!
None of your defined numbers are binary numbers
#define add 000001 // octal literals because it starts with 0
#define sub 000010
#define jmp 000111
#define IMM 10000 // decimal literals
#define ADDR 10001
None of them are binary representations as you assume in your code.
Since C++14 you can write a binary literal as (example from https://en.cppreference.com/w/cpp/language/integer_literal):
int b = 0b101010; // C++14
In general I would strongly advise you to not use #define, unless you deliberatly choose to get all the trouble that come with using macros.
To access the defined values I just re-write them like this:
#define add 0x1
#define sub 0x2
#define jmp 0x7
#define IMM 0x10
#define ADDR 0x11
And now it`s working fine.
Related
I'm working on code to compute CRC32 using the hardware CRC support that's built into the ARM Cortex-M4 processor. For reference, there's an application note that describes the hardware here:
http://www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00068118.pdf
Basically, you write 32-bits of data at a time to a memory-mapped register (CRC_DR), and then you read the resulting CRC back from the same address. However, the CRC this produces is quite different than the standard result that the software CRC32 libraries produce. I finally found someone who had written code that manipulates the Cortex result to produce the "standard" result:
http://www.cnblogs.com/shangdawei/p/4603948.html
My code (shown below and adapted from the above solution) now produces the "standard" result, but I suspect there are more calls to function ReverseBits than are actually necessary. I'm hoping someone can tell me if it can be simplified.
Thanks!
Dan
#define RCC_BASE 0x40023800
#define RCC_AHB1ENR *((uint32_t *) (RCC_BASE + 0x30))
#define CRC_BASE 0x40023000
#define CRC_DR *((volatile uint32_t *) (CRC_BASE + 0x00))
#define CRC_IDR *((volatile uint32_t *) (CRC_BASE + 0x04))
#define CRC_CR *((volatile uint32_t *) (CRC_BASE + 0x08))
uint32_t ARMcrc32(void *data, uint32_t bytes)
{
uint32_t *p32 = data ;
uint32_t crc, crc_reg ;
RCC_AHB1ENR |= 1 << 12 ; // Enable CRC clock
CRC_CR |= 0x00000001 ; // Reset the CRC calculator
while (bytes >= 4)
{
CRC_DR = ReverseBits(*p32++) ;
bytes -= 4 ;
}
crc_reg = CRC_DR ;
crc = ReverseBits(crc_reg) ;
if (bytes > 0)
{
uint32_t bits = 8 * bytes ;
uint32_t xtra = 32 - bits ;
uint32_t mask = (1 << bits) - 1 ;
CRC_DR = crc_reg ;
CRC_DR = ReverseBits((*p32 & mask) ^ crc) >> xtra ;
crc = (crc >> bits) ^ ReverseBits(CRC_DR);
}
return ~crc ;
}
I built a virtual machine in C. And for this I have the Instruction
pushc <const>
I saved the command and the value in 32 Bit. The First 8 Bit are for the command and the rest for the value.
8 Bit -> Opcode
24 Bit -> Immediate value
For this I make a macro
#define PUSHC 1 //1 is for the command value in the Opcode
#define IMMEDIATE(x) ((x) & 0x00FFFFFF)
UPDATE:
**#define SIGN_EXTEND(i) ((i) & 0x00800000 ? (i) | 0xFF000000 : (i))**
Then I load for testing this in a unsigned int array:
Update:
unsigned int code[] = { (PUSHC << 24 | IMMEDIATE(2)),
(PUSHC << 24 | SIGN_EXTEND(-2)),
...};
later in my code I want to get the Immediate value of the pushc command and push this value to a stack...
I get every Instruction (IR) from the array and built my stack.
UPDATE:
void exec(unsigned int IR){
unsigned int opcode = (IR >> 24) & 0xff;
unsigned int imm = (IR & 0xffffff);
switch(opcode){
case PUSHC: {
stack[sp] = imm;
sp = sp + 1;
break;
}
}
...
}
}
Just use a bitwise AND to mask out the lower 24 bits, then use it in the case:
const uint8_t opcode = (IR >> 24) & 0xff;
const uint32_t imm = (IR & 0xffffff);
switch(opcode)
{
case PUSHC:
stack[sp] = imm;
break;
}
I shifted around the extraction of the opcode to make the case easier to read.
I would want to create a macro to get easy access to a single bit from a structure like the following:
typedef union
{
struct
{
uint8_t bit0 : 1;
uint8_t bit1 : 1;
uint8_t bit2 : 1;
uint8_t bit3 : 1;
uint8_t bit4 : 1;
uint8_t bit5 : 1;
uint8_t bit6 : 1;
uint8_t bit7 : 1;
};
uint8_t raw;
} Bitfield;
I have a bi-dimensional array(x) of this structure. The best that I could make was :
#define xstr(r,c,b) str(r,c,b)
#define str(r,c,b) (x[r][c].bit##b)
#define getBit(bitCollum,row)(xstr(row,(bitCollum/8),(bitCollum%8))
When I try to use the macro like uint8_t a = getBit(15,2); it will expand to
uint8_t a = ( ( img [ 2 ] [ ( 15 / 8 ) ] . bit 15 % 8 ) );
and I would want to create a structure that will expand to:
uint8_t a = ( ( img [ 2 ] [ ( 15 / 8 ) ] . bit7 ) );
Is this even possible?
bitCollum and row will always be literal integers; the expression will not be run in a loop or something like that.
EDIT:
After seeing that it wasn't possible i looked at the disassembly of a simple increment and I saw different instructions but for my surprise the masking was faster.
` x.raw = 0b10101001;
00000040 LDI R24,0xA9 Load immediate
00000041 STD Y+8,R24 Store indirect with displacement
uint8_t y = 0b10101001;
00000042 LDI R24,0xA9 Load immediate
00000043 STD Y+1,R24 Store indirect with displacement
uint16_t xSum=0;
00000044 STD Y+3,R1 Store indirect with displacement
00000045 STD Y+2,R1 Store indirect with displacement
uint16_t ySum=0;
00000046 STD Y+5,R1 Store indirect with displacement
00000047 STD Y+4,R1 Store indirect with displacement
xSum+=x.bit3;
00000048 LDD R24,Y+8 Load indirect with displacement
00000049 BST R24,3 Bit store from register to T
0000004A CLR R24 Clear Register
0000004B BLD R24,0 Bit load from T to register
0000004C MOV R24,R24 Copy register
0000004D LDI R25,0x00 Load immediate
0000004E LDD R18,Y+2 Load indirect with displacement
0000004F LDD R19,Y+3 Load indirect with displacement
00000050 ADD R24,R18 Add without carry
00000051 ADC R25,R19 Add with carry
00000052 STD Y+3,R25 Store indirect with displacement
00000053 STD Y+2,R24 Store indirect with displacement
ySum+=y&0b00010000;
00000054 LDD R24,Y+1 Load indirect with displacement
00000055 MOV R24,R24 Copy register
00000056 LDI R25,0x00 Load immediate
00000057 ANDI R24,0x10 Logical AND with immediate
00000058 CLR R25 Clear Register
00000059 LDD R18,Y+4 Load indirect with displacement
0000005A LDD R19,Y+5 Load indirect with displacement
0000005B ADD R24,R18 Add without carry
0000005C ADC R25,R19 Add with carry
0000005D STD Y+5,R25 Store indirect with displacement
0000005E STD Y+4,R24 Store indirect with displacement `
Instead of the structures, use simple bytes - uint8_t
#define GETBIT(r,c) (img[r][(c) >> 3] & (1 << ((c) & 7)))
#define SETBIT(r,c) img[r][(c) >> 3] |= (1 << ((c) & 7))
#define CLRBIT(r,c) img[r][(c) >> 3] &= ~(1 << ((c) & 7))
However, if you want it efficient, you better avoid manipulating things one bit at a time.
It could be that I'm missing some "trick", but, AFAIK, this is not possible.
Basically, you're trying to compute a value and then append it to some token. The problem here is that the preprocessor doesn't do computations (except in #if and such statements). So, for example:
#define X2(A,B) A##B
#define X(A,B) X2(A,B)
int x = X(13 + 4, 4);
this will expand to:
int x = 13 + 44;
and not to:
int x = 174;
If you try to put parenthesis, you will just get compiler errors, 'cause this is not valid:
int x = (13+4)4;
While processing macros, everything is just a "string" (token) to the preprocessor. Actually, it is the compiler that will, in the example above, see that 13 + 44 is constant and compile that as int x = 57; (well, an intelligent compiler, I've worked with some C compilers in my day that were not so smart :) ).
#define GET_BIT(VAR8,IDX) ((VAR8>>IDX) & 1)
int main(void){
unsigned char c=3;
int i;
printf("Bits of char %d: ",c);
for(i=0; i<8;i++){
printf("%d ",GET_BIT(c,i));
}
printf("\n");
return 0;
}
I have to write a program which sends files from a UDP client to a UDP server.
I have no problems sending data over this kind of connection, but a different matter is taking care of our protocol specification. Maybe someone can help me to understand how to realize these steps correctly:
Step 1:
Send first package: First Element of data array, int value for type specification.
After that the filename size (without path) as unsigned short.
After that the filename (without path) as C-string without NULL termination.
At least there should be the file length as unsigned integer.
Both sizes should be sent in network byte order!
All this information has to be in one single datagram. So actually I just have an unsigned char array which is sent by the client.
I thought I could assign anything to this one array, one after another. But don't think this works.
Step 2:
Send second package: Type specification as int as first element. Then the data of the given file (I saved it as unsigned char array too). That's much easier because I just have information which is easy to put in my unsigned char array.
So, I think, there must be a way to build a generic method or something like that, that returns a datagram, so I can fill in all data types I want to have in this datagram and get my datagram ready to send, you know?
It would be nice if someone has an idea on how I can "prepare" a datagram in the right way - the most important thing for me is:
Which kind of type should my datagram array have? To send it with sendTo() it needs to be a unsigned char array, right?
Best greetings, and thanks a lot!
you can use a structs to map your informations for example:
struct Packet {
unsigned short filnemanesize;
char [MAX_LEN] filename;
unsigned int filesize;
//etc.
}
You can send this struct directrly through the socket
send(mySocket, (void*) myStruct, sizeof(myStuct), 0);
BUT the compilers uising a different member packing, so it would happen that the server sees different data. (Its safe for exact the same plattform/compiler).
A common practice is, serializing your struct into a char array, here is a complete example:
#include <stdint.h>
#include <iostream>
#include <WinSock2.h>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
#define MAX_FILENAME_SIZE 10
typedef struct {
int32_t type;
uint16_t fileNameSize;
uint32_t extraPadding; //just some manual padding (example)
char fileName [MAX_FILENAME_SIZE];
uint32_t fileSize;
} Packet;
const size_t PACKET_RAW_SIZE = sizeof(int32_t) + sizeof(uint16_t) + (MAX_FILENAME_SIZE * sizeof(char)) + sizeof(uint32_t);
size_t serialize(unsigned char * const dst, const Packet * src) {
/* Protocoll Specification:
[Type:4Byte][fileNameSize:2Byte][fileName:nByte][fileSize:4Byte]
*/
uint16_t offset = 0;
//type
memcpy(dst + offset, &src->type, sizeof(src->type));
offset += sizeof(src->type);
//fileNameSize
memcpy(dst + offset, &src->fileNameSize, sizeof(src->fileNameSize));
offset += sizeof(src->fileNameSize);
//fileName
memcpy(dst + offset, &src->fileName, ntohs(src->fileNameSize));
offset += ntohs(src->fileNameSize);
//fileSize
memcpy(dst + offset, &src->fileSize, sizeof(src->fileSize));
offset += sizeof(src->fileSize);
return offset;
}
int main(int argc, _TCHAR* argv[])
{
unsigned char buffer[PACKET_RAW_SIZE];
for (int i = 0; i < PACKET_RAW_SIZE; buffer[i++] = 0); //nice visual effect, not nessasarry
Packet myPacket;
myPacket.type = htonl(0xAFFEAFFE);
myPacket.fileNameSize = htons(4); //convert 4 to network byte order as required
myPacket.fileSize = htonl(42);
myPacket.fileName[0] = 'T';
myPacket.fileName[1] = 'E';
myPacket.fileName[2] = 'S';
myPacket.fileName[3] = 'T';
myPacket.extraPadding = 0xFFFFFFFF; // better visual effect
cout << "struct Packet size: " << sizeof(Packet) << endl;
cout << "PACKET_RAW_SIZE: " << PACKET_RAW_SIZE << endl;
size_t bufferSize = serialize(&buffer[0], &myPacket);
cout << "myPacket: " << endl;
for (int i = 0; i < sizeof(myPacket); i++) {
unsigned char * ptr = (unsigned char *) &myPacket;
cout << std::showbase << std::hex << "Offset: " << i * sizeof(unsigned char) << "\t Value: " << (uint16_t)*(ptr + i) << "\t" << *(ptr + i) << endl;
}
cout << endl;
cout << "buffer: " << endl;
for (int i = 0; i < bufferSize; i++) {
cout << std::showbase << std::hex << "Offset: " << i * sizeof(unsigned char) << "\t Value: " << (uint16_t)buffer[i] << "\t" << buffer[i] << endl;
}
cout << endl;
return 0;
}
/*
Program-Output:
struct Packet size : 28
PACKET_RAW_SIZE : 20
myPacket :
Offset : 0 Value : 0xaf »
Offset : 0x1 Value : 0xfe ■
Offset : 0x2 Value : 0xaf »
Offset : 0x3 Value : 0xfe ■
Offset : 0x4 Value : 0
Offset : 0x5 Value : 0x4 ♦
Offset : 0x6 Value : 0x9a Ü
Offset : 0x7 Value : 0xf ☼
Offset : 0x8 Value : 0xff
Offset : 0x9 Value : 0xff
Offset : 0xa Value : 0xff
Offset : 0xb Value : 0xff
Offset : 0xc Value : 0x54 T
Offset : 0xd Value : 0x45 E
Offset : 0xe Value : 0x53 S
Offset : 0xf Value : 0x54 T
Offset : 0x10 Value : 0x52 R
Offset : 0x11 Value : 0x17 ↨
Offset : 0x12 Value : 0x9f ƒ
Offset : 0x13 Value : 0xf ☼
Offset : 0x14 Value : 0x1 ☺
Offset : 0x15 Value : 0
Offset : 0x16 Value : 0
Offset : 0x17 Value : 0
Offset : 0x18 Value : 0
Offset : 0x19 Value : 0
Offset : 0x1a Value : 0
Offset : 0x1b Value : 0x2a *
buffer :
Offset : 0 Value : 0xaf » | <= type
Offset : 0x1 Value : 0xfe ■ |
Offset : 0x2 Value : 0xaf » |
Offset : 0x3 Value : 0xfe ■ |
Offset : 0x4 Value : 0 | <= fileNameSize
Offset : 0x5 Value : 0x4 ♦ | (network byte order)
Offset : 0x6 Value : 0x54 T | <= fileName
Offset : 0x7 Value : 0x45 E |
Offset : 0x8 Value : 0x53 S |
Offset : 0x9 Value : 0x54 T |
Offset : 0xa Value : 0 | <= fileSize
Offset : 0xb Value : 0 | (network byte order)
Offset : 0xc Value : 0 |
Offset : 0xd Value : 0x2a |
*/
You can see, the buffer contains only the required data. And the mapping is as required in the spec. All manual and compiler padding is not inside the buffer.
htons/htonl are used for byte order conversions.
Even better is some kind of text serialization such as xml or json (imho)
I made a function to set or clear a specific number of bits in a DWORD. My function works. I don't need help making it work. However, I am wondering if the method I've chosen to do it is the fastest possible way.
It's rather hard for me to explain how this works. There are two arrays containing DWORDs that are filled with bits on the left and right side of the DWORD (with all binary 1's). It makes a mask with all the bits filled except for the ones I want to set or clear, and then sets them with bitwise operators based on that mask. It seems rather complicated for such a simple task, but it seems like the fastest way I could come up with. It's much faster than setting them bit by bit.
static DWORD __dwFilledBitsRight[] = {
0x0, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF, 0x7FFFFFF, 0xFFFFFFF, 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
};
static DWORD __dwFilledBitsLeft[] = {
0x0, 0x80000000, 0xC0000000, 0xE0000000, 0xF0000000, 0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000, 0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000, 0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000, 0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000, 0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00, 0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0,
0xFFFFFFF0, 0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF
};
// nStartBitFromLeft must be between 1 and 32...
// 1 is the bit farthest to the left (actual bit 31)
// 32 is the bit farthest to the right (actual bit 0)
inline void __FillDWORDBits(DWORD *p, int nStartBitFromLeft, int nBits, BOOL bSet)
{
DWORD dwLeftMask = __dwFilledBitsLeft[nStartBitFromLeft - 1]; // Mask for data on the left of the bits we want
DWORD dwRightMask = __dwFilledBitsRight[33 - (nStartBitFromLeft + nBits)]; // Mask for data on the right of the bits we want
DWORD dwBitMask = ~(dwLeftMask | dwRightMask); // Mask for the bits we want
DWORD dwOriginal = *p;
if(bSet) *p = (dwOriginal & dwLeftMask) | (dwOriginal & dwRightMask) | (0xFFFFFFFF & dwBitMask);
else *p = (dwOriginal & dwLeftMask) | (dwOriginal & dwRightMask) | 0;
}
How about:
// Create mask of correct length, and shift to the correct position
DWORD mask = ((1ULL << nBits) - 1) << pos;
// Apply mask (or its inverse)
if (bSet)
{
*p |= mask;
}
else
{
*p &= ~mask;
}
It's pretty likely that simple bitwise operations will be faster than table lookup on any modern processor.
Note: Depending on the relationship between DWORD and long long on this platform, you may need special handling for the case where nBits == sizeof(DWORD)*8. Or if nBits==0 is not a possibility, you could just do DWORD mask = ((2ULL << (nBits - 1)) - 1) << pos;.
Update: It's been mentioned that the if could potentially be slow, which is true. Here's a replacement for it, but you'd need to measure to see if it's actually any faster in practice.
// A bit hacky, but the aim is to get 0x00000000 or 0xFFFFFFFF
// (relies on two's-complement representation)
DWORD blanket = bSet - 1;
// Use the blanket to override one or other masking operation
*p |= (blanket | mask);
*p &= ~(blanket & mask);
This is the way I'd do it. I'd break it into two functions, setbits() and clearbits(). Steps broken out for clarity, and I'm sure it can be far more optimized.
This version is dependent on 32-bit code as it stands. Also, in my world, bit 0 is the rightmost bit. Your mileage may vary.
setbits( DWORD *p , int offset , int len )
{
// offset must be 0-31, len must be 0-31, len+offset must be 0-32
int right_shift = ( !len ? 0 : 32 - (len+offset) ) ;
int left_shift = offset ;
DWORD right_mask = 0xFFFFFFFF >> right_shift ;
DWORD left_mask = 0xFFFFFFFF << left_shift ;
DWORD mask = left_mask & right_mask ;
*p |= mask ;
return ;
}
clearbits( DWORD *p , int offset , int len )
{
// offset must be 0-31, len must be 0-31, len+offset must be 0-32
int right_shift = ( !len ? 0 : 32 - (len+offset) ) ;
int left_shift = offset ;
DWORD right_mask = 0xFFFFFFFF >> right_shift ;
DWORD left_mask = 0xFFFFFFFF << left_shift ;
DWORD mask = ~( left_mask & right_mask ) ;
*p &= mask ;
return ;
}
I stumbled across this improved version whilst looking for something else today. Courtesy of Sean Anderson's Bit Twiddling Hacks at Stanford University:
// uncomment #define to get the super scalar CPU version.
// #define SUPER_SCALAR_CPU
void setbits( unsigned int *p , int offset , int len , int flag )
{
unsigned int mask = ( ( 1 << len ) - 1 ) << offset ;
#if !defined( SUPER_SCALAR_CPU )
*p ^= ( - flag ^ *p ) & mask ;
#else
// supposed to be some 16% faster on a Intel Core 2 Duo than the non-super-scalar version above
*p = (*p & ~ mask ) | ( - flag & mask ) ;
#endif
return ;
}
Much depends on your compiler, though.