Recent

Author Topic: Does anyone know how to write an assembler in pascal?  (Read 1519 times)

TYDQ

  • Full Member
  • ***
  • Posts: 102
Does anyone know how to write an assembler in pascal?
« on: June 17, 2024, 02:50:48 pm »
These times I am attempting to add an assembler for x64 in my UEFIPascalOS.But I cannot attain a completely intergrated translating assembly to machine code chart.Does anyone have open source assembler in pascal or other programming languages or reference so I can look up the translation chart for making the assembler for my OS?

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: Does anyone know how to write an assembler in pascal?
« Reply #1 on: June 17, 2024, 04:37:39 pm »
Factor out the assembler parts from the compiler sources?
If I smell bad code it usually is bad code and that includes my own code.

TYDQ

  • Full Member
  • ***
  • Posts: 102
Re: Does anyone know how to write an assembler in pascal?
« Reply #2 on: June 17, 2024, 05:33:49 pm »
Factor out the assembler parts from the compiler sources?
Ok,I will try this and if I have any question,I will be posted to this topic.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8025
Re: Does anyone know how to write an assembler in pascal?
« Reply #3 on: June 17, 2024, 10:24:16 pm »
Classically, it was a relatively simple lookup operation: the opcode determined some bitfields in the ouyput byte(s). \the operands determined the remainder.

These days, particularly on "mature" architectures like x86 and even moreso x86_64, it is vastly more complex since in many cases the operands determine at least part of the opcode selection.

The bottom line is that you need to be intimately familiar with the processor documentation, which in the case of x86_64 runs to around three volumes (plus multiple commentaries from Intel, AMD and others).

I would suggest that the best way of going about this would be to research a suitable subset of the overall x86 opcode set that will be adequate for what you actually need to do. For example, if you only need basic computation and control transfers it would be a fairly safe bet to leave out anything related to floating point and SIMD operations.

A couple of years ago I implemented a disassembler for the V20/V30 chips which are 16-bit and substantially simpler than what we have today. It was fairly hard work.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
Re: Does anyone know how to write an assembler in pascal?
« Reply #4 on: June 17, 2024, 10:46:55 pm »
well, I did a 6502/6510 one...  :D in 1981 in basic.it is not that difficult for e.g. 8086 too but complexity has indeed skyrocketed.
Simple assemblers are still not difficult to write if you confine to a subset and indeed use a tabled approach.
Or, horror, do it with $EF or db notation and all the opcodes from memory. (I could do that, once, party trick)

BTW there are many asm compiler sources available written in pascal for all kind of cpu's.
« Last Edit: June 17, 2024, 11:04:21 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

440bx

  • Hero Member
  • *****
  • Posts: 4731
Re: Does anyone know how to write an assembler in pascal?
« Reply #5 on: June 17, 2024, 11:04:38 pm »
These times I am attempting to add an assembler for x64 in my UEFIPascalOS.But I cannot attain a completely intergrated translating assembly to machine code chart.Does anyone have open source assembler in pascal or other programming languages or reference so I can look up the translation chart for making the assembler for my OS?
I don't know if what I'm going to mention will do the trick for you but, the Zydis disassembler (written in C) includes a way of assembling as well.

Here some example code (translated from their EncodeFromScratch example) which assembles two instructions (which are later executed):
Code: Pascal  [Select][+][-]
  1.   // Assemble `mov rax, 0x1337`.
  2.  
  3.   RtlZeroMemory(@req, sizeof(req));
  4.  
  5.   with req do
  6.   begin
  7.     mnemonic              := ZYDIS_MNEMONIC_MOV;
  8.     machine_mode          := ZYDIS_MACHINE_MODE_LONG_64;
  9.     operand_count         := 2;
  10.     operands[0].&type     := ZYDIS_OPERAND_TYPE_REGISTER;
  11.     operands[0].reg.value := ZYDIS_REGISTER_RAX;
  12.     operands[1].&type     := ZYDIS_OPERAND_TYPE_IMMEDIATE;
  13.     operands[1].imm.u     := $1337;
  14.   end;
  15.  
  16.   AppendInstruction(@req, @write_ptr, @remaining_length);
  17.  
  18.   // Assemble `ret`.
  19.  
  20.   RtlZeroMemory(@req, sizeof(req));
  21.  
  22.   with req do
  23.   begin
  24.     mnemonic     := ZYDIS_MNEMONIC_RET;
  25.     machine_mode := ZYDIS_MACHINE_MODE_LONG_64;
  26.   end;
  27.  
  28.   AppendInstruction(@req, @write_ptr, @remaining_length);
  29.  
The problem is going to be that you need their dll (which is where the code that does all the work is) and that may not be a workable solution depending on when you need the capability. 

Anyway, hopefully that gives you something to consider. Zydis is open source and offered under the MIT license (which is just about as permissive a licence can get.)

Just FYI, the call to AppendInstruction is a very thin wrapper over the Zydis function  ZydisEncoderEncodeInstruction which creates the binary code based on the description in the "req" variable/structure.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

TYDQ

  • Full Member
  • ***
  • Posts: 102
Re: Does anyone know how to write an assembler in pascal?
« Reply #6 on: June 18, 2024, 03:58:58 am »
Classically, it was a relatively simple lookup operation: the opcode determined some bitfields in the ouyput byte(s). \the operands determined the remainder.

These days, particularly on "mature" architectures like x86 and even moreso x86_64, it is vastly more complex since in many cases the operands determine at least part of the opcode selection.

The bottom line is that you need to be intimately familiar with the processor documentation, which in the case of x86_64 runs to around three volumes (plus multiple commentaries from Intel, AMD and others).

I would suggest that the best way of going about this would be to research a suitable subset of the overall x86 opcode set that will be adequate for what you actually need to do. For example, if you only need basic computation and control transfers it would be a fairly safe bet to leave out anything related to floating point and SIMD operations.

A couple of years ago I implemented a disassembler for the V20/V30 chips which are 16-bit and substantially simpler than what we have today. It was fairly hard work.

MarkMLl
OK,I will look up the developer manual of AMD and intel since It can be searched on the Internet.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8025
Re: Does anyone know how to write an assembler in pascal?
« Reply #7 on: June 18, 2024, 09:14:17 am »
OK,I will look up the developer manual of AMD and intel since It can be searched on the Internet.

Be warned that that's going to be... heavy.

As I have already tried to emphasise, you'd be far better starting off with a subset. Use e.g. 32-bit real if you can get away with it.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018