1

So I'm trying to write an emulator for Arm instructions in a special context and I would like to understand the full behavior of the program counter in an Arm processor. Specs have the statement below

The Program Counter (PC) is accessed as PC (or R15). It is incremented by the size of the instruction executed (which is always four bytes in ARM state). Branch instructions load the destination address into the PC. You can also load the PC directly using data processing instructions. For example, to branch to the address in a general purpose register, use: MOV PC, R0 During execution, the PC does not contain the address of the currently executing instruction. The address of the currently executing instruction is typically PC–8 for ARM, or PC–4 for Thumb.

From this, I understand that when the ADDS PC, #-4 was executed PC would be at +4 or +8, so if it's at +4 would the program go into an infinite loop?

aperezfals
  • 1,341
  • 1
  • 10
  • 26
  • 1
    If you want to write an emulator, I think you should get a ARM device to be able to test weird use cases in real. – Guillaume Petitjean Aug 08 '19 at 15:09
  • For the record, Keil microvision provides an instruction set simulator for ARM cores, isn't it what you need ? – Guillaume Petitjean Aug 08 '19 at 15:10
  • 3
    You should specify which ARM architecture you're trying to emulate. For example, at least on ARMv5 and older there's no `ADD(S) PC,#imm` in Thumb mode as far as I'm aware. There is `ADDS PC, PC, #imm` in ARM mode (which is marked as `UNPREDICTABLE` in certain execution modes, because of the way it affects the program status register). I suggest you get one of the ARM reference manuals, such as ARM DDI 0100E. – Michael Aug 08 '19 at 15:45
  • the program counter is two instructions ahead yes which is 8 bytes for arm mode and 4 bytes for non-thumb2 extension thumb mode. so for arm since you cant do this in thumb, adds pc,#-8. which my assembler turned into subs pc,pc,#8, yes that should just be an infinite loop. but if you are making an emulator do you care? read the source register value, which for the program counter you make a fake one that is two ahead, subtract 8 from it and then store the result in the specified register. – old_timer Aug 09 '19 at 00:18
  • you have to decide how you want to handle the pc in your emulator, do you want to keep the pc pointing at the next instruction, and do the two ahead thing when the pc is an operand or do you want to try to always keep the pc ahead, and when you fetch you have to subtract one instruction to get the address to fetch from. or even better do both. have a pc_next that is used for fetching and points at the next instruction, when a branch or the subs pc,pc,#8 happens you use pc for the operand and the result goes to pc_next – old_timer Aug 09 '19 at 00:20
  • for each fetch at the beginning of the next instruciton pc_next fetches. and you compute pc from pc_next then move pc_next forward one. not unlike how you will find it implemented in logic. – old_timer Aug 09 '19 at 00:22
  • and as pointed out you need to decide what core you want emulate. and/or what warts do you want to declare unpredictable because one of the cores couldnt handle it in the past, or want to make your sim just fix the problem and keep going, print a warning, etc...also note that some of the UNPREDICTABLES were actually predictable and used to determine if a core had been stolen/cloned. possibly not intentionally, but a side effect. – old_timer Aug 09 '19 at 00:24
  • @old_timer The question is about adding #-4 not #-8, and it is not about "how do I implement an infinite loop?" - if I'm reading correctly, that is. So, I believe, if we rewrite the instr to (ARM mode) ADD PC, PC, #-4, the answer should be - *the PC is loaded with the address of the instruction that follows(?)*, i.e. this is effectively a NOP. Ready to be corrected as I can't really test this at the moment. – tum_ Aug 09 '19 at 06:11
  • @old_timer Re-reading the last paragraph - the question is actually badly worded, so it's not clear what exactly is asked... – tum_ Aug 09 '19 at 06:15
  • -4 will not give you an infinite loop...no. – old_timer Aug 09 '19 at 14:47
  • there isnt a thumb instruction for this certainly not ones that would result with pc+4 during execution so that leaves only arm and arm like thumb is two ahead at +8 so pc -4 is the next instruction after the add. I had already answered that question and moved on to what would actually create an inifnite loop. none of these are a nop – old_timer Aug 09 '19 at 14:49
  • this is all documented all you have to do is look. granted if you look at the documentation you find the legalese that says you cant use this documentation to do what you are doing.. but the language changes between revs of the documentation. – old_timer Aug 09 '19 at 14:51

0 Answers0