Are There Mistakes in My ARM Cortex-M Machine Code Exercises?

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Arm Code Machine
Click For Summary

Discussion Overview

The discussion revolves around the correctness of machine code exercises for the ARM Cortex-M architecture. Participants are reviewing specific machine code instructions and their corresponding effects on register values and flag registers (NZVC). The scope includes both theoretical understanding and practical application of machine code operations.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a series of machine code instructions and requests feedback on their correctness.
  • Another participant questions whether the values of the registers for each opcode were supposed to be indicated, suggesting a potential misunderstanding of the task.
  • A third participant clarifies that they filled out a table based on the provided code and were tasked with calculating the result of R = A + B and the corresponding flag values.
  • One participant expresses uncertainty about specific instructions, such as the meaning of the opcode SUBS and the difference between ORR and OR, while affirming that the results for some operations appear correct.
  • There is a request for clarification on the flag values and what was originally provided to the participant who filled out the table.

Areas of Agreement / Disagreement

Participants express varying levels of confidence in the correctness of the machine code exercises, with some entries appearing acceptable while others raise questions. There is no consensus on the correctness of all entries, and several points remain contested or unclear.

Contextual Notes

Unresolved aspects include specific definitions of opcodes like SUBS and ORR, as well as the initial conditions or instructions given for the flag values, which may affect the interpretations of the results.

sandy.bridge
Messages
797
Reaction score
1

Homework Statement


Hey guys. I am just learning machine code for Cortex-M. There are a few exercises within the textbook, however, I am not entirely sure if I am doing them correctly. If you could possible look over my work to see if there are any mistakes, I would greatly appreciate it! They are supposed to be relatively simple, however, I am still not entirely sure.

Each line represents a specific set of machine code. I will provide the code adjacent in brackets.

R0----R1----R2
0x45-----------(MOV R0, #0x45)
0x45 0x02------(MOV R1, #0x02)
0x45 0x02 0x47 (ADD R2, R0, R1)
0x45 0x02 0x43 (SUB R2, R0, R1)
0x45 0x02 0x43 (SUBS R2, R0, R1)
0x45 0x02 0x05 (AND R2, R0, #0x01)
0x45 0x02 0x05 (ORR R0, R0, #0x01)
0x0F 0x02 0x05 (MOV R0, #0x0F)
0x0F 0x0F 0x05 (MOV R1, #0x0F)
0x0F 0x0F 0x05 (CMP R1, R0)
0x0F 0x0F 0x00 (SUBS R2, R0, R1)



The other part had to do with with flags. We are given A and B, two 8-bit numbers, where R = A + B. We have to determine what R would equal, and what the values of the flag registers would be (NZVC).

A---------- B---------- R---------- NZVC
10--------100---------110-------- 0000
0x40------0xA2--------0xC0-------0000
0xC3------0x6F--------0x32-------0001
100-- ----(-100)-------0---------- 0100
110-------146---------0---------- 0101
50---------206-------- 0---------- 0101
 
Physics news on Phys.org
sandy.bridge said:

Homework Statement


Hey guys. I am just learning machine code for Cortex-M. There are a few exercises within the textbook, however, I am not entirely sure if I am doing them correctly. If you could possible look over my work to see if there are any mistakes, I would greatly appreciate it! They are supposed to be relatively simple, however, I am still not entirely sure.

Each line represents a specific set of machine code. I will provide the code adjacent in brackets.

R0----R1----R2
0x45-----------(MOV R0, #0x45)
0x45 0x02------(MOV R1, #0x02)
0x45 0x02 0x47 (ADD R2, R0, R1)
0x45 0x02 0x43 (SUB R2, R0, R1)
0x45 0x02 0x43 (SUBS R2, R0, R1)
0x45 0x02 0x05 (AND R2, R0, #0x01)
0x45 0x02 0x05 (ORR R0, R0, #0x01)
0x0F 0x02 0x05 (MOV R0, #0x0F)
0x0F 0x0F 0x05 (MOV R1, #0x0F)
0x0F 0x0F 0x05 (CMP R1, R0)
0x0F 0x0F 0x00 (SUBS R2, R0, R1)
What's your question? Were you supposed to indicate the values of the three registers for each of the op codes above?
sandy.bridge said:
The other part had to do with with flags. We are given A and B, two 8-bit numbers, where R = A + B. We have to determine what R would equal, and what the values of the flag registers would be (NZVC).

A---------- B---------- R---------- NZVC
10--------100---------110-------- 0000
0x40------0xA2--------0xC0-------0000
0xC3------0x6F--------0x32-------0001
100-- ----(-100)-------0---------- 0100
110-------146---------0---------- 0101
50---------206-------- 0---------- 0101
 
Hi Mark44,

The table was left blank and we were merely given the code indicated in brackets. I then filled out the table accordingly. For the second portion, R = A + B. We were supposed to fill out R and indicate what value the flag registers would be. Sorry I was no more clear.
 
I'm not familiar with Cortex-M, so caveat emptor.

Most of your entries in the table look OK, but there are a few that I'm uncertain of.

For this one -- (SUB R2, R0, R1) -- I'm guessing that the CPU calculates R0 - R1 and stores the result in R2. If so, then what you have for the registers looks fine.

For this one -- (SUBS R2, R0, R1) -- I don't know what the opcode SUBS means, signed subtract?

For this one -- 0x45 0x02 0x05 (ORR R0, R0, #0x01) -- I suspect this is different from OR, but I don't know how it's different. In any case, it looks to be ORing what's in R0 with 0x01, which would just result in the same thing, since what's in R0 is odd.

For the table with flag values, what were you given and what did you fill in?