{"slug": "assembly-code-to-machine-code-arm", "title": "Assembly Code to Machine Code (ARM)", "summary": "This article explains how ARM assembly instructions are translated into binary machine code. It breaks down the components of an ARM instruction, such as condition codes, opcodes, and source/destination registers, using examples like `ADD R5, R6, R7` and `SUB R2, R3, #0xFF0`. The summary details how each part of the assembly instruction maps to specific binary fields, including handling immediate values and shift operations.", "body_md": "Summary of the video https://www.youtube.com/watch?v=ttJZjP0p_uE\nI have heard assembly code is the closest of how humans can write in a fashion that machines can understand. I have never understood how. Here's the bit of how the translation occurs from assembly instruction to machine code.\nIn ARM Assembly, the following is the translation from assembly code to binary. After looking at each section in detail, we will try to translate ARM operations into binary operations that machine (ARM chip in this case) understands.\nCond\nSOME NOTES\n-\nADD\noperation takes Opcode 1110 AL\nfor always.\n- Flag bit is usually the previous operation that results in some bit set. In\nADDEQ\nit will execute if the previous instruction set Z flag to true (1).\nop\nI\n- It stands for immediate\n- Example,\nADD, R1, R2, #0x28\n- If a constant / label is used, then the I field is set to 1\ncmd\n- This is the operation that we are well aware of\n- It is ARM data processing instructions\nS\n- Setting S here means, we want the status of operation\n- For example,\nADDS R1, R2, R3\nmeans, put on the status like whether the operation will be Zero, Negative, Carry (this will be shown in CPSR register - which we will cover but not in this article)\n-\nADDS\noperation will set S status to 1 and let us start tracking the status.\nRn (19:16)\n- It is called first source register\n- In\nADD R1, R2, R3\nRn is R2\n- Hence get's binary value of 0010 in 19:16\nRd (15:12)\n- It is also called destination register\n- In\nADD R1, R2, R3\nRd is R1\n- Hence it gets binary value of 0001 in 15:12\nSrc2 (11:0)\n-\nSecond Source: Can be a variety of things\na) Immediate\nb) Register\nc) Register-shifted Register\nImmediate\n-\nrot is for rotation. Mnemonic for it is\nROR\nfor rotate right.\n- NOTE: It is subject to rotate right by twice the value in the rotate field\n- 11:8 bits represent the amount of rotation to right of their immediate counterpart (7:0)\nRegister\n- shamt5 represents amount of shift whether left or right\n- sh is the shift operators - ops table at the bottom\n- Rm is the register of target whose values being shifted\nRegister-shifted Register\n- Rs is the register that holds the amount of shift\n- Rm is the target register whose value is being shifted\nsh table\nSTARTING WITH THE EASY ONE ADD R5, R6, R7\nLet's unpack one section at a time\n- cond is ALWAYS hence 1110, since there's no condition to prevent ADD being done.\n- op is 00\n- I is 0 (there's no immediate values here)\n- cmd is ADD which translates to 0100\n- No Status indicator ADD(S) S is omitted, hence S is 0\n- Rn is source, hence R6, 0110\n- Rd is destination, hence R5, 0101\n- shamt5 is 00000, since there's no shift\n- Sh is 00 as there's no shift\n- Rm is src2 hence, 7. 0111.\nCombining them leads to\nNicely formatted binary here:\n1110 0000 1000 0110 0101 0000 0000 0111\nCare to convert to hex?\n0xE0865007\nSLIGHTLY HARDER: ADD R5, R6, R7, LSR #4\n- we pick immediate variety for shift operations since because\n#4\nis a literal value\n- Most are the same except fields 11:0\n-\nLSR\nhas sh code as 01\n-\nLSR\namount is 4 so shamt5 is 00100\n- Rm is 7, hence 0111\nLET'S DO MORE: ADD R0, R1, #42\n- The third one is immediate, hence\n- I is set to 1\n- Src2 becomes immediate format (rot for 11:8 and immediate value 7:0)\nBRING SOME MORE! SUB R2, R3, #0xFF0\n- Rd is 2, Rn is 3, imm - 0xff0\n- SUB has 0010 code.\n- OH NO, BUT #0xFF0 does not fit in 8 bit.\n- That's ok. That's what rot is for.\n- 0xFF0 is 0000 0000 0000 0000 0000 1111 1111 0000\n- 0xFF is 0000 0000 0000 0000 0000 0000 1111 1111\n- How many shift to right will make 0xFF the 0xFF0?\n- 1 shift right is 1000 0000 0000 0000 0000 0000 0111 1111\n- Following? Let's shift right a little more.\n- 4 shift right is 1111 0000 0000 0000 0000 0000 0000 1111\n- 8 shift right is 1111 1111 0000 0000 0000 0000 0000 0000\n- guess what? it takes 24 shift right to get 0xFF0!\n- So, rot should be 12 since by our rule the actual rotation is twice the value at rot.\n- Hence, the 11:8 bit values will be\n1100\nand 7:0 1111 1111\n- which is just a representation of 0xff0 into 8 bit number combined with rotation.\nWHAT ABOUT THIS? LSL R0, R9, #7\n- WAIT WAIT... LSL is not in the command table. How am I supposed to put in the bit field 24:21?\n- Thanks Rakesh, the creator of the video: Basically LSL is equivalent to this:\nMOV R0, R9, LSL #7\n- Wait again... Rakesh says R9 is not Rn... Hm.. I thought it would be the same as how SUB was done above.\n- In the MOV operation, that's not the case, as per user guide armasm user guide page 333\nMOV R0, R9, LSL #7\napplies to the following syntax: MOV{S}{cond} Rd, Operand2\nwhere operand2 is (according to page 244 the same guide) can be Register with optional shift.. Hence, on page 246 of the guide it says, register with optional shift, is Rm{, shift}\n.\n- Still following?\n- Hence, R9 here is Rm, where Rm is the register holding the data for the second operand.\n- Hence, Rn here is 0 and Rm is 9\n- (BY THE WAY THE REFERENCE I'M TALKING ABOUT IS armasm User Guide Version 6.6) - the latest is here\nOK TAKE A BREAK AND COME BACK! ROR R3, R5, #21\n- GUESS WHAT.. SHIFT AGAIN! Which means Rm is 5\n- This is equivalent to\nMOV R3, R5, ROR, #21\n- Same translation step for\nLSL\nabove..\nKEY TAKEAWAY:\n- There's no one rule for all in the translations. Sometimes, you have to look up command table sometimes you will face operation that are not in one table hence, need to break down the command.\n- But, all should be translated to binary otherwise, machine won't understand! So, let's stick to the basics and see if we can translate!!!!!\n- Good news and bad news: You and I have learned how to translate,, not entirely but seen a bit of it... But these translation steps will be also different in A64 architecture but... we learned how to apply our knowledge in some way... Some methods must be similar... must be..\nOK AFTER YOUR DINNER... LSR R4, R8, R6\n- This time the shift amount is in R6.\n- Does that make R8, the source register the Rn?\n- NOPE!!\n- This is equivalent to\nMOV R4, R8, LSR, R6\n- R6 is Rs haha! Found it!\n- Rm is 8 hohoho\nPHEW LET'S SLEEP AFTER THIS... ASR R5, R1, R12\n- What is Rd, Rn, Rm, Rs?? Is some of them 0? Which one?\n- Answer below:", "url": "https://wpnews.pro/news/assembly-code-to-machine-code-arm", "canonical_source": "https://dev.to/hwangs12/assembly-code-to-machine-code-arm-57ga", "published_at": "2026-05-22 18:22:17+00:00", "updated_at": "2026-05-22 18:32:18.037063+00:00", "lang": "en", "topics": ["hardware", "semiconductor", "developer-tools", "research"], "entities": ["ARM"], "alternates": {"html": "https://wpnews.pro/news/assembly-code-to-machine-code-arm", "markdown": "https://wpnews.pro/news/assembly-code-to-machine-code-arm.md", "text": "https://wpnews.pro/news/assembly-code-to-machine-code-arm.txt", "jsonld": "https://wpnews.pro/news/assembly-code-to-machine-code-arm.jsonld"}}