tgtakaoka / libasm

C++ library of assembler / disassembler that can run on embedded system
Other
28 stars 1 forks source link

expresions in assembler numeric evaluation & c header output & stdout #107

Closed awilliams-au closed 2 years ago

awilliams-au commented 2 years ago

Is it possible to do math in mediate constants?

For example take the low or high bytes from a label's address. In 6502 lda # high byte of 'return' sta some high byte vector location lda# low byte of 'return' sta some low byte vector location etc

.return RTI

In the BBC Micro BASIC's build in 6502 assembler I would use LDA # return DIV 256 LDA # return MOD 256

I would also find it useful if asm could generate c header file like this. Probably the prefix string 'at' could be a parameter. const unsigned char atFFE0[] = { 0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8, 0xF7, 0xF6, 0xF5 };

Also '-' as a file name for 'use std out' would be handy to pipeline the processing of the hex file output for example.

Excellent work though, looking forward to using ins8060 and ins8070 modes.

tgtakaoka commented 2 years ago

Sorry for my late reply.

The libasm assembler can evaluate standard arithmetic and logical operators. For the details, please refer test_expr_base.cpp for examples.

For calculating high and low order byte from 16bit values, you can do it using bitwise and and shift.

  cpu 6502
  lda #(return >> 8)
  sta some_high_byte_vector_location
  lda #(return & $FF)
  sta some_low_byte_vector_location
return: rti

Of course you can use / for division and % for modulo if you prefer.

Stating from version 1.64, you can define functions to do it more concisely.

  cpu 6502
hi: function val, (val >> 8)
lo: function val, (val & $FF)
  lda #hi(return)
  sta some_high_byte_vector_location
  lda #lo(return)
  sta some_low_byte_vector_location
return: rti
tgtakaoka commented 2 years ago

Unfortunately I don't plan to add C-array format for assembler output, though you can add it by extending BinFormatter class in cli/binformatter.h.