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:
- Read Code (16 bits) and get offset (10 bits LSB)
- Check Z flag (0 or 1)
- If Z = 0, move to next section of Code (PC++)
- 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?