1

I'm programming a MSP430 in C language as a simulation of real microcontroller (or emulator if you want). When I prepare test.s file for MSP430:

    .text
.global main
    .type   main, @function

main:
    mov #8,r4;
    add r5,r5;    // r5 = 0
    jz l1;
    add r6,r6;    // r6 = 0
    jz l2;
    push r6;     // for break purpose


l1:
    mov #0x1234,r8; 
    push  r10;

l2:
    mov #0x5678,r9;
    push r11;

I got such verification (by MSP430) file test.lst:

test.elf:   file format elf32-msp430

Disassembly of section .text:

00000000 <main>:
   0:   34 42           mov #8, r4      ;r2 As==11
   2:   05 55           rla r5          ;
   4:   00 24           jz  $+2          ;abs 0x6
   6:   06 56           rla r6          ;
   8:   00 24           jz  $+2          ;abs 0xa
   a:   06 12           push    r6      ;

0000000c <l1>:
   c:   38 40 34 12     mov #4660,  r8  ;#0x1234
  10:   0a 12           push    r10     ;

00000012 <l2>:
  12:   39 40 78 56     mov #22136, r9  ;#0x5678
  16:   0b 12           push    r11     ;

The problem is that JUMPS (i.e. JZ) are never going to happen. Whatever Z flag is set before JZ, code is moving through JZ. My emulation of JZ is:

  1. Read Code (16 bits) and get offset (10 bits LSB)
  2. Check Z flag (0 or 1)
  3. If Z = 0, move to next section of Code (PC++)
  4. Else, set PC = PC + 2*offset

In binary file, generated by MSP430, the offset value is exactly 0 (should be more than 0 to move PC into another Code section).

How to make Jumps work correctly? Or maybe generation of binary file is broken?

CL.
  • 173,858
  • 17
  • 217
  • 259
ziom
  • 199
  • 3
  • 11
  • 2
    While some call C the "assembler of high-level languages", this is about true Assembler. Do not add unrelated tags! – too honest for this site Jan 16 '16 at 18:40
  • @Olaf, nope, I do this in C language. Assembler is only used for preparing binary input files (which could be a cause of the problem). – ziom Jan 16 '16 at 18:43
  • 2
    You only show Assembler output. C is not relevant for this. At most, it is a matter of the compiler. – too honest for this site Jan 16 '16 at 18:46
  • Perhaps I misunderstand something, but it's not clear why do you think the ZERO bit should be set (as in the comment `r5=0`)? The instruction "add r5, r5" simply adds r5 to itself, it does not set it zero in the general case. – kfx Jan 16 '16 at 19:09
  • As as side note, you do know of https://github.com/mspsim/mspsim, do you? – kfx Jan 16 '16 at 19:10
  • @kfx: It does very well. Please see the MSP430 instruction set. Note that is quite typical for many CPUs. – too honest for this site Jan 16 '16 at 19:13
  • Too little information. The assembler shown: is that from ready-linked code or the object file? Also show what the debugger's disassembly says. That is the most useful information. – too honest for this site Jan 16 '16 at 19:16
  • @kfx I want to create processor in C (with input and output), this site is worthless for my objectives – ziom Jan 16 '16 at 19:18
  • @Olaf in that case `mspsim` is broken. However, I also checked the instruction set and did not see any relevant comments, so that seems doubtful. – kfx Jan 16 '16 at 19:37
  • 2
    @kfx: 1) OP did not state which compiler/assembler he uses. 2) Nor did he say if the file has been relocated or not, which might make a difference. 3) And to see which flags the `ADD` instruction changes - thus if a `JZ` is usefull or not, you just have to see the instruction description in any MSP430 family user's guide. – too honest for this site Jan 16 '16 at 20:56
  • 1
    @kfx http://www.ti.com/lit/ug/slau049f/slau049f.pdf at 85 page. And from page #40: " Zero bit. This bit is set when the result of a byte or word operation is 0 and cleared when the result is not 0." – ziom Jan 16 '16 at 21:17
  • @Olaf, I use TI-GCC (http://www.ti.com/tool/msp430-gcc-opensource) – ziom Jan 16 '16 at 21:19
  • @Olaf you may want to detract your first comment in light of ziom's link unless you have conflicting information. – kfx Jan 17 '16 at 09:59
  • @kfx Why would I? That is clearly not about C. – too honest for this site Jan 17 '16 at 15:39
  • @Olaf perhaps we're misunderstanding each another. The claim that "add r5, r5" sets the register to zero (which you supported) looks misleading to me, you should back it up with a reference or remove it. – kfx Jan 17 '16 at 17:38
  • @kfx: The claim was it sets the `ZERO` flag in the status register according to the outcome of the operation. And that is very true and quite common for most CPU architectures. I **did** back it up; please read my comments again! It is not my fault if you are not able to find the documentation I mentioned - TI's website is quite well-structured. If you still insist, it is youir's to prove me wrong. – too honest for this site Jan 18 '16 at 01:06
  • @Olaf ok, so you actually agree with my first comment. Makes sense now. BTW, `mspsim` is not a compiler or an assembler. – kfx Jan 18 '16 at 11:06
  • @kfx: I don't see how I could. Maybe it is badly worded, but you state that `add` will not set the flag(s). On second reading, I'm not sure what you mean with "general case", though. The instructions always sets/updates the flag - either to `0` or `1`. The MSP430 does not have ALU instructions which do not update any flag. – too honest for this site Jan 18 '16 at 12:34

1 Answers1

2

Your test.elf file actually is an object file, so it still contains unresolved relocations.

After assembling and linking this file (with the C compiler, to get the C startup code that calls main), the jumps are correct:

$ msp430-gcc -mmcu=msp430f5529 -minrt -o test.elf test.s
$ msp430-objdump -d test.elf
...
0000454e <main>:
    454e:   34 42           mov #8,     r4  ;r2 As==11
    4550:   05 55           rla r5          ;
    4552:   03 24           jz  $+8         ;abs 0x455a
    4554:   06 56           rla r6          ;
    4556:   04 24           jz  $+10        ;abs 0x4560
    4558:   06 12           push        r6  ;

0000455a <l1>:
    455a:   38 40 34 12     mov #4660,  r8  ;#0x1234
    455e:   0a 12           push        r10 ;

00004560 <l2>:
    4560:   39 40 78 56     mov #22136, r9  ;#0x5678
    4564:   0b 12           push        r11 ;
...

It is possible to generate a binary from this:

$ msp430-objcopy -O binary test.elf test.bin

However, the resulting file is not very useful because it does not contain its start address. You would be better off creating (and then using in your simulator) an Intel hex file:

$ msp430-objcopy -O ihex test.elf test.ihex
CL.
  • 173,858
  • 17
  • 217
  • 259