diff --git a/assembler.pas b/assembler.pas index 179b6e8..f226dd0 100644 --- a/assembler.pas +++ b/assembler.pas @@ -9,7 +9,9 @@ type LblRec = record Addr: word; Lbl: ansistring; + Line: integer; Offset: integer; + Resolved: boolean; end; var @@ -17,6 +19,7 @@ var Line, DatOrg: ansistring; //Line of assembly and data or org element Elem: array [0 .. 4] of ansistring; //Parsed elements Lbl, Ref: array [0 .. $ffff] of LblRec; //Label and reference tables + AllRefsResolved: boolean; //Whether there are any references to nonexistent labels Addr, BP, SP, EP: word; //Address argument and byte, start, and end pointers Bin: array [0 .. $ffef] of byte; //Assembled binary Prog: file of byte; //Program file @@ -136,7 +139,9 @@ begin //Store the reference in the table Ref [Count] .Addr := BP + A; Ref [Count] .Lbl := Elem [I]; + Ref [Count] .Line := LP; Ref [Count] .Offset := Offset; + Ref [Count] .Resolved := false; //Placeholder Addr := 0; Offset := 0; @@ -222,6 +227,7 @@ begin if BP + Ref [Count] .Offset <= $ffff then begin Bin [Ref [Count] .Addr] := (BP + Ref [Count] .Offset) shr 8; Bin [Ref [Count] .Addr + 1] := (BP + Ref [Count] .Offset) and $00ff; + Ref [Count] .Resolved := true; end else RefError; end @@ -373,6 +379,18 @@ begin until eof (); + //Check that all references to labels were properly resolved + AllRefsResolved := true; + Count := 0; + while Ref [Count] .Lbl <> '' do begin + if Ref [Count] .Resolved = false then begin + writeln ('Error (line ', Ref [Count] .Line, '): label not found: ', Ref [Count] .Lbl); + AllRefsResolved := false; + end; + Count := Count + 1; + end; + if AllRefsResolved = false then halt; + //Set the end pointer and reset the byte pointer to the start of the program EP := BP; BP := SP;