How Do You Make An Assembler?

X86Assembly

X86 Problem Overview


I'd like to make a simple x86 assembler. I'm wondering if there's any tutorials for making your own assembler. Or if there's a simple assembler that I could study.

Also, I wonder what tools are used in looking at and handling the binary/hex of programs.

X86 Solutions


Solution 1 - X86

Solution 2 - X86

Just a very tiny piece of code in Delphi 7.

{$APPTYPE CONSOLE}
program assembler;
uses sysutils;
const
s1=#0#77#1#90#59#64#4#80#1#69#3#76#1#1#1#1#14#224#2#15#1#1#1#11#1#1#1#1#1#64#13+
#116#1#16#13#64#3#16#4#2#3#1#8#3#2#10#7#32#4#2#7#3#5#16#4#16#5#1#10#16#13#16#3+
#184#124#184#5#16#3#184#5#2#15#96#3#224#173#52#1#16#3#40#1#16#23#65#1#16#3#80#1+
#16#7#75#1#69#1#82#1#78#1#69#1#76#1#51#1#50#1#46#1#68#1#76#1#76#4#71#1#101#1+
#116#1#83#1#116#1#100#1#72#1#97#1#110#1#100#1#108#1#101#4#87#1#114#1#105#1#116+
#1#101#1#67#1#111#1#110#1#115#1#111#1#108#1#101#1#65#2#72#1#101#1#108#1#108#1+
#111#1#44#1#32#1#87#1#111#1#114#1#108#1#100#1#33#1#13#1#10#5#0;
s3=#1#185#1#7#4#136#1#195#1#128#1#227#1#15#1#193#1#216#1#4#1#128#1#251#1#9+
#1#118#1#3#1#128#1#195#1#39#1#128#1#195#1#48#1#136#1#153#1#96#1#16#1#64#2#73#1+
#125#1#228#1#106#2#104#1#112#1#16#1#64#2#106#1#8#1#104#1#96#1#16#1#64#2#106#1+
#245#1#255#1#21#1#40#1#16#1#64#2#80#1#255#1#21#1#44#1#16#1#64#2#195;
var
  f:file of byte;p,i:integer;o:string;
  t:text;line:string;
procedure w(s: string);
begin
  i:=1;
  while i<length(s) do begin
    inc(p,ord(s[i]));
    setlength(o, p);
    o[p]:=s[i+1];
    inc(i,2);
  end;
end;
procedure al(b: byte);
var
  a: longword;pc: pchar;
begin
  a := strtoint(line); pc:=@a;
  o := o + chr(b) + pc^ + (pc+1)^ + (pc+2)^ + (pc+3)^; inc(p,5); // mov eax, imm32
end;
begin
  assign(f,'out.exe');
  rewrite(f);
  p:=1;
  w(s1);
  assignfile(t, ''); reset(t);
  while not eof(t) do begin
    readln(t, line); line := trim(line);
    if copy(line,1,8) = 'mov eax,' then begin
      system.delete(line,1,8);
      al($b8); // mov eax, imm32
    end
    else if copy(line,1,8) = 'add eax,' then begin
      system.delete(line,1,8);
      al($05); // add eax, imm32
    end
    else if copy(line,1,8) = 'and eax,' then begin
      system.delete(line,1,8);
      al($25); // and eax, imm32
    end
  end;
  closefile(t);
  w(s3);
  blockwrite(f,o[1],p); close(f);
end.

The assembler understands only three different assembler codes "mov eax,immed32", "add eax,immed32", "and eax,immed32" and no data nor labels. It will produce a tiny Windows PE executable which outputs eax in hex at the end.

Attention: In my case avira free antivirus doesn't like the output. It's a false positive. I had to switch off the real time protection. Check the result with a debugger if you are uncertain if this is malware (It's not!)

Solution 3 - X86

I wrote one a long time ago. It is as simple as getting the x86 assembler ref guide from Intel, and writing the bytes to a .com file (for windows). I wish i could find my old forum post I made on it. It was written in D++. Just goes to show you can do it in any language. Just tokenize your string and translate it.

Solution 4 - X86

As far as example code goes...

I don't know of any "simple" assemblers, though.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmudgenView Question on Stackoverflow
Solution 1 - X86clawsView Answer on Stackoverflow
Solution 2 - X86NilsBView Answer on Stackoverflow
Solution 3 - X86Jeremy BoydView Answer on Stackoverflow
Solution 4 - X86piotoView Answer on Stackoverflow