1

HI all, I am trying to design an assembler in C based on some instruction sets. I want to read a assembly language file containing instructions. IN the hex file there is hex data, integer data and negative integer data. I need to convert that data into 8 bit hexadecimal data , out of which 6 bit represents hexadecimal data and the remaining 2 bit will be of opcode. For example ; ldc 0x1000 output: 00100000 (opcode of ldc is 00) ldc -3; output : fffffd00 (6 bit 2s complement of +3).

I am trying ltoa to convert the integer into hexadecimal, but its giving string as output so i am not able to append 0s . PLease suggest urgentl

1
  • Why can you not append 0s to a string? Also, I suggest looking at the sscanf() and sprintf() functions (or variants) instead, they are probably going to be your best friends in a project like this Commented Dec 24, 2010 at 12:32

1 Answer 1

2

You seem to confuse bits with nibbles. One hexadecimal digit represents 4 bits. The opcode can store a 24 bit constant, 6 nibbles. Read the value from the string with strtol() and left-shift it by 8. Or with the opcode.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but i need to read data and convert into 8 bit hexadecimal data out of which 6 bit will represent the actual data and two LSBs will represent opcode.
You need to think about how you are going to store 0x1000 (4096) into 6 bits (2^6 = 64). At least 13 bits are required. No machine exists that has 2 bits for an opcode, that allows for only 4 instructions. I can only assume you are confusing bits and bytes. One byte requires two hexadecimal digits. en.wikipedia.org/wiki/Nibble

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.