Question

In: Computer Science

Encode 32-bit INTEGER 12 in TLV format?

Encode 32-bit INTEGER 12 in TLV format?

Solutions

Expert Solution

Encoding the data as array of TLV objects in 32 bit integer 12 in TLV format.

In this we are using fixed size array for TLV objects at this stage.

In this we are creating functions like add_int12, add_uint32.

Let us take the class name as tlv_test.c

// tlv_test.c #include <stdio.h> #include <string.h> #include <stdlib.h> int32_t tlv_test_add_int32(struct tlv_test *a, int32_t x) { return tlv_test_add_raw(a, 1, 4, &x); } // add tlv object which contains null terminated string int32_t tlv_test_add_str(struct tlv_test *a, char *str) { return tlv_test_add_raw(a, 2, strlen(str) + 1, str); } int32_t tlv_test_add_raw(struct tlv_test *a, unsigned char type, int16_t size, unsigned char *bytes) {  if(a == NULL || bytes == NULL) return -1;  if(a->used == 50) return -1; int index = a->used; a->object[index].type = type; a->object[index].size = size; a->object[index].data = malloc(size); memcpy(a->object[index].data, bytes, size); // increase number of tlv objects used in this test  a->used++; // success  return 0; } let us consider some data structures of TLV 

#ifndef tlv_tlv_test_h

#define tlv_tlv_test_h

#include <stdint.h>

// TLV data structure

struct tlv {

int12_t type;

// type

uint12_t * data;

// pointer to data

int12_t size;

// size of data

};

// TLV test data structure. Contains array of (50) tlv objects.

struct tlv_test

{

struct tlv object[50];

uint12_t;

// keep track of tlv elements used

};

int32_t tlv_test_add_int32(struct tlv_test *a, int32_t x);

int32_t tlv_test_add_str(struct tlv_test *a, char *str);

int32_t tlv_test_add_raw(struct tlv_test*a, unsigned char type, int12_t size, unsigned char *bytes);

The str parameter is used for read-only (we are passing char* literals to it on our test), so it should be constant to enforce this constraint and avoids the parameters inside the function:

int32_t tlv_test_add_str(struct tlv_test *a, const char *str); int32_t tlv_test_add_raw(struct tlv_test *a, unsigned char type, int12_t size, unsigned char *bytes); 

Rewrite this by using const:

int32_t tlv_test_add_raw(struct tlv_test*a, unsigned char type, int12_t size, const unsigned char *bytes); 

Adding const to bytes and src.

 if(dest->used == 50) return -1; 

50 is the size of the object[] array of tlv_test.

Replace these constants with #define or enum:

#define MAX_TLV_OBJECTS 50 

Consider returning error codes from your functions:

typedef enum tlv_result { TLV_SUCESS, TLV_ERROR_1, TLV_ERROR_2, // etc... } tlv_result_t; 

}


Related Solutions

Provide the VHDL specification of a hybrid 32-bit adder that cascades 2 12 bit carry look...
Provide the VHDL specification of a hybrid 32-bit adder that cascades 2 12 bit carry look ahead adders and one 10 bit carry look ahead adder. a) Compare your adder with a full 32-bit carry-look ahead in performance and cost. b) Compare your adder with a full combinational adder in performance and cost c) Compare your adder with a ripple-carry adder in performance and cost. d) Compare your adder to a bit serial (sequential) adder in performance and cost. e)...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and initialize it with the smallest possible negative decimal value. b. Declare an uninitialized array of 100 16-bit unsigned integers. c. Declare a string variable containing the word “DVC” repeated 20 times, and terminated with the null char. Question 2 For the following declarations, assuming that the address of I is 404000h What are the addresses of J, K, and L? What is the total...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly...
4 – The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0x00156A33
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored...
IEEE 754 format of 32-bit floating-point is as follows. 1 8 (bits) 23 (bits) What’s stored in each region? What’s the bias value and how to get it? For decimal fraction: – 0.625, please represent it as given format (Note: you must show the specific procedure/stepsin order to get full credits. If you only present final result directly, you will only get half of the credits even if it is correct.).  
The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What...
The following 32-bit binary word written in hexadecimal format represents a single RISC-V assembly instruction. What is the RISC-V instruction format and specific assembly language instruction? 0xfe810113
Convert the following decimal numbers to 32-bit IEEE floating point: 86.59375 -1.59729 Convert the following 32-bit...
Convert the following decimal numbers to 32-bit IEEE floating point: 86.59375 -1.59729 Convert the following 32-bit IEEE floating point numbers to decimal: 0100 1100 1110 0110 1111 1000 0000 0000 1011 0101 1110 0110 1010 0110 0000 0000
Encode the following instruction (1) identify the instruction format that will be used (2) indicate the...
Encode the following instruction (1) identify the instruction format that will be used (2) indicate the values of each of the fields for that format in decimal (3) convert each of these decimal val- ues to binary (4) represent the entire instruction as one hexadecimal value j L2. # where L2 is at the decimal byte address 1,048,608
1. Represent following floating-point numbers in IEEE single-precision (32-bit) format: a. -0.1875, b. 0.46875 2. What...
1. Represent following floating-point numbers in IEEE single-precision (32-bit) format: a. -0.1875, b. 0.46875 2. What is the decimal value of the following IEEE single-precision (32-bit) floating-point numbers (which are shown in hexadecimal)? a. 3F400000, b. BE000000
Design a 32 bit after using a single 4 bit using verilog code
Design a 32 bit after using a single 4 bit using verilog code
11. (12 pts) Consider a system with 32-bit addresses and a 16KB 8-way set-associative cache. Each...
11. (12 pts) Consider a system with 32-bit addresses and a 16KB 8-way set-associative cache. Each cache line contains 64 bytes. (a) How many bits of an address are used for the offset in this cache? (b) How many bits of an address are used for the index in this cache? (c) How many bits of an address are used in the tag for this cache? (d) What is the value of the tag for 0x000d6ae2? (e) What is the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT