B Compiler (hcbcomp)

Translates the B programming language into assembly for Z80, 8086, 8080, and 8085 targets. B is a typeless systems programming language — the predecessor of C — where all data is a 16-bit machine word.

BinaryTarget CPURuntimePointer Size
hcbcomp-z80Zilog Z80CP/M, MSX-DOS16-bit
hcbcomp-8086Intel 8086MS-DOS (.COM)16-bit
hcbcomp-8086exeIntel 8086MS-DOS (.EXE)32-bit (far)
hcbcomp-8080Intel 8080CP/M16-bit
hcbcomp-8085Intel 8085CP/M16-bit

Usage

hcbcomp-{z80,8086,8080,8085} [options] <input.b>

Options:
  -o <file>       Output assembly file (default: a.s)
  -I <dir>        Add include directory for preprocessor
  --help, -h      Show help

Compilation Pipeline

source.b[Preprocessor]  →  #define, #ifdef, #include
    ↓
[Compiler]      →  Lexer → Parser → Code Generator
    ↓
[Peephole]     →  Instruction-level optimization
    ↓
[Output]       →  Assembly (.s) file

Calling Convention

AspectConvention
Parameter passingStack, right-to-left (C convention)
Evaluation orderRight-to-left (two-pass fseek, no reversal step)
Stack cleanupCaller cleans (add sp, n*2)
Return valueHL (Z80) / AX (8086)
Frame pointerIX (Z80) / BP (8086) / BC (8080/8085)

Peephole Optimizer

TargetPatternBeforeAfterGain
Z80Local load9 instr2 instr (ld r,[ix+N])-7
Z80Local store (imm)12 instr3 instr-9
Z80Stack cleanupld hl,N; add hl,sp; ld sp,hlinc sp×N-4B
Z80Reg copypush bc; pop hlld h,b; ld l,c-7T
Z80Swap around inc spex de,hl; inc sp×N; ex de,hlinc sp×N-4B
8086LEA+deref3 instr1 instr (mov ax,[bp+N])-2
8086Global deref3 instr1 instr (mov ax,[label])-2
8086Store imm to local5 instr1 instr (mov word [bp+N],imm)-4
8086Push/Pop copy2 instr1 instr (mov reg,reg)-1
8086Zero registermov ax,0xor ax,ax-1B
8086exe32-bit push/pop4 instr2 instr (mov cx,dx; mov bx,ax)-2
8086exeLEA+deref3 instr1 instr-2
8086exeZero reg (dx)mov dx,0xor dx,dx-1B
8086exe32-bit negnot ax;not dx;add ax,1;adc dx,0neg dx;neg ax;sbb dx,0-2B
8080/8085FP copypush h; pop bmov b,h; mov c,l-10T
8080/8085Store immpush h; lxi h,N; pop d; xchgxchg; lxi h,N; xchg-1B -16T

B Language Quick Reference

FeatureSyntax
Comments/* block */ // line
Global variablevar; arr[256];
Local variableauto x, y;
External symbolextrn printf;
Functionname(p1, p2) { ... }
if/elseif (x) stmt; else stmt;
whilewhile (x) stmt;
forfor (init; cond; incr) stmt;
returnreturn expr;
breakbreak;
Arithmetic+ - * / % ++ --
Comparison< > <= >= == !=
Logical&& || !
Bitwise& | ^ ~ << >>
Assignment= += -= *= /= %= &= |= ^=
Preprocessor#define #ifdef #ifndef #include
Inline assemblyasm("ld a, 0");

[Full B Language Reference] · [Peephole Details] · [Calling Convention]