Recent

Author Topic: assembler  (Read 3020 times)

Weiss

  • Full Member
  • ***
  • Posts: 181
assembler
« on: July 17, 2024, 04:39:49 am »
where would you use assembler, in your Pascal code? Which compiler to download? I understand there are different flavors of assembler, thus the next question is - which assembler works in conjunction with Pascal? I have a book and this itch to start learning away, but it would help if there are some pointers. What tasks are better implemented in assembler. Please share your thoughts, folks.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 547
Re: assembler
« Reply #1 on: July 17, 2024, 04:59:31 am »
Chapter Three of the Free Pascal Programmers Manual is titled "Using Assembly Language."  Find it here:

http://downloads.freepascal.org/fpc/docs-pdf/user.pdf   or
https://sourceforge.net/projects/freepascal/files/Documentation/3.2.2/

I came across a few examples of its use in the rtl today I think (using asm to speed up some string operations) but I don't remember exactly where.

The best place to enjoy ASM anymore, in my opinion, is in a microcomputer environment.  And even MCUs  are getting so big and complex that learning to use their ASM  is a major commitment to studying a chip's docs .  Lots of fun though in the old days with 8-bit devices like the Z80.


« Last Edit: July 17, 2024, 05:06:08 am by Curt Carpenter »

TRon

  • Hero Member
  • *****
  • Posts: 3510
Re: assembler
« Reply #2 on: July 17, 2024, 05:03:12 am »
where would you use assembler, in your Pascal code?
Personally nowhere.

The compiler is pretty good at optimizing generated code and while it might have been fruitful to count cycles for old processors it is pretty much undoable to optimize for modern processors to such an extent that is becomes noticeable. Ofc for your particular use-case and setup you might be able to optimize the crap out of things but that doesn't guarantee for the same code to run as smooth on another setup or other use-case.

That is besides the usual suspects such as cross-platform compatibility.

Quote
Which compiler to download?
The ones supported by FPC for a particular platform ?

Quote
I understand there are different flavors of assembler, thus the next question is - which assembler works in conjunction with Pascal?
Depends on the target.

Quote
I have a book and this itch to start learning away, but it would help if there are some pointers. What tasks are better implemented in assembler. Please share your thoughts, folks.
You might perhaps also want to have a look at this and this
This tagline is powered by AI

440bx

  • Hero Member
  • *****
  • Posts: 4686
Re: assembler
« Reply #3 on: July 17, 2024, 05:44:29 am »
where would you use assembler, in your Pascal code?
Only in extremely rare cases where implementing the task in Pascal is very inefficient and the task is repeated hundreds of thousand of times (if not millions.)

I strongly encourage you and anyone to learn assembler but, not for the purpose of using it in your Pascal code but, for the purpose of having a better understanding of how things work.  For instance, to be reasonably proficient in assembler you must know how the CPU works and also, very often, many details  about how the O/S works.    Writing a reasonably large program in assembler also forces you to think about program structure and to make things simple because, otherwise the code becomes unmanageable. That focus on simplicity is an asset when using a high level language such as Pascal, the code is simpler, easier to maintain and usually (but not always) much faster than convoluted, tricky code.

For instance, if you know assembler and some Pascal code you've written isn't working as expected, you can inspect the assembly code generated by the compiler.  Occasionally, that will reveal a bug in the compiler and, more often than not, it will reveal that what the compiler "understood" you wanted isn't what you really wanted, IOW, there is no compiler bug, you didn't tell the compiler what you wanted clearly or exactly enough.

Back some time ago, Iczelion had a website with good assembly tutorials.  I believe the web site is no longer available but the tutorials are still around in various web sites.  That and choosing an assembler (MASM, NASM, FASM, other) with a user forum would be a good staring point. 

One thing you should be aware of is that, you'll very likely have to read the intel/AMD reference manuals.  Assembler requires having reasonable knowledge about the CPU, something which high level languages make mostly unnecessary.  In assembly, it is most definitely necessary.  You'll also need to learn quite a bit about the O/S in order to produce assembly programs that the O/S will load and run.

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

TRon

  • Hero Member
  • *****
  • Posts: 3510
Re: assembler
« Reply #4 on: July 17, 2024, 05:53:45 am »
All true 440bx but I believe the original question was "in your Pascal code"

Though you can link the objects together to make the assembler generated object "part" of your Pascal code I have the notion that that is not what TS had in mind.

Perhaps Weiss is able to elaborate on that and his (specific) use case in case there is any.
This tagline is powered by AI

440bx

  • Hero Member
  • *****
  • Posts: 4686
Re: assembler
« Reply #5 on: July 17, 2024, 06:08:52 am »
All true 440bx but I believe the original question was "in your Pascal code"
I believe the first paragraph in my post answered the original question.  At this point, it is exceedingly rare that I would use assembly in Pascal code.

The rest of the post is to make the point that in spite of not using assembler in Pascal code, there are still very good reasons to invest the time and effort to learn it.  I wanted to encourage him to learn it but, not for the purpose of using it in Pascal code.

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

TRon

  • Hero Member
  • *****
  • Posts: 3510
Re: assembler
« Reply #6 on: July 17, 2024, 06:18:26 am »
The rest of the post is to make the point that in spite of not using assembler in Pascal code, there are still very good reasons to invest the time and effort to learn it.  I wanted to encourage him to learn it but, not for the purpose of using it in Pascal code.
I wholeheartedly share that thought.

For me it was the most exciting thing to learn and which is able to teach a lot about how processors work. Also the thought-process and structuring of code is completely different in comparison to higher level languages. Even more exciting and informative when you code for an older cpu/system such as mentioned by Curt.
« Last Edit: July 17, 2024, 06:21:09 am by TRon »
This tagline is powered by AI

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11864
  • FPC developer.
Re: assembler
« Reply #7 on: July 17, 2024, 07:36:32 am »
I have some assembler in my FPC/Delphi work code. (which is actually our product, not some internal system), and they are all SSE/AVX SIMD code to do operations on a whole image at once. (usually copyiing from a source image to a dest image)

Weiss

  • Full Member
  • ***
  • Posts: 181
Re: assembler
« Reply #8 on: July 17, 2024, 08:57:22 am »
thank you all. Learning assembler was on my mind for a while, for a pure sake of knowledge. But, if there is little actual use for it in my Pascal code, than I can foresee the lack of positive feedback from new things I learned.

MarkMLl

  • Hero Member
  • *****
  • Posts: 7904
Re: assembler
« Reply #9 on: July 17, 2024, 10:42:34 am »
The rest of the post is to make the point that in spite of not using assembler in Pascal code, there are still very good reasons to invest the time and effort to learn it.  I wanted to encourage him to learn it but, not for the purpose of using it in Pascal code.
I wholeheartedly share that thought.

FWIW, I agree. The really important thing is to know enough "assembler" that you can tell the compiler to output an intermediate listing and see how it's laying things out: initialised- vs non-initialised variables and so on.

I should of course refer to that as meta-assembler etc., but the idea I'm trying to get across is how the directives etc. work even if the actual opcodes are obscure.

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: 16019
  • Censorship about opinions does not belong here.
Re: assembler
« Reply #10 on: July 17, 2024, 11:59:32 am »
meta-assembler etc.,
You mean a high-level language compiler.. :D

Anyway, an assembler depends on a processor family or even something else like java byte code or wasm.
Quite a few are implemented as basm in FPC and they are quite good too.
Other remark:
An assembler need not optimize, in fact never does: as a programmer you are on your own to write optimal assembler code and often a high-level language compiler does it MUCH better than you or I can. In yesteryears that was not true, but now it is.
« Last Edit: July 17, 2024, 12:06:20 pm by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Fred vS

  • Hero Member
  • *****
  • Posts: 3407
    • StrumPract is the musicians best friend
Re: assembler
« Reply #11 on: July 17, 2024, 12:35:56 pm »
Hello.

When cross-compiling fpc 3.3.1 source form Linux amd64 to FreeBSD aarch64 using fpcupdeluxe and his provided aarch64-freebsd-as the compilation is ok. Note that I have to copy  /from_fpupdeluxe_cross_bin/aarch64-freebsd13-as in /usr/bin/ and rename it as aarch64-freebsd13as otherwise there is a "assembler not found" error.

But if I try to re-compile fpc 3.3.1 source on a real FreeBSD 14.0 aarch64 machine using the provided "as" assembler, the compilation fails with operand mismatch errors (see picture).
Must I use a other assembler?
« Last Edit: July 17, 2024, 02:35:30 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

MarkMLl

  • Hero Member
  • *****
  • Posts: 7904
Re: assembler
« Reply #12 on: July 17, 2024, 12:54:48 pm »
meta-assembler etc.,
You mean a high-level language compiler.. :D

No, I most definitely do not.

I mean the directives etc. which- as I said- determine how things are laid out, and which have some limited degree of similarity between assembler for different targets (unlike the opcodes themselves).

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: 16019
  • Censorship about opinions does not belong here.
Re: assembler
« Reply #13 on: July 17, 2024, 04:14:49 pm »
But if I try to re-compile fpc 3.3.1 source on a real FreeBSD 14.0 aarch64 machine using the provided "as" assembler, the compilation fails with operand mismatch errors (see picture).
Must I use a other assembler?
Almost always use the toolchain provided by arm. fpcupdeluxe usually picks that up. The latest stable is 13.3 and is available as a separate download from the arm developers website:
https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads.
I would only do that if the freebsd version is behind.

(You can also find the cross-tools there to and from almost anything, so take the time to see what you want)
If I smell bad code it usually is bad code and that includes my own code.

Fred vS

  • Hero Member
  • *****
  • Posts: 3407
    • StrumPract is the musicians best friend
Re: assembler
« Reply #14 on: July 17, 2024, 04:50:39 pm »
But if I try to re-compile fpc 3.3.1 source on a real FreeBSD 14.0 aarch64 machine using the provided "as" assembler, the compilation fails with operand mismatch errors (see picture).
Must I use a other assembler?
The latest stable is 13.3 and is available as a separate download from the arm developers website:
https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads.
I would only do that if the freebsd version is behind.

Thanks Thaddy for that precious link, I will explore it deeply.

Quote
(You can also find the cross-tools there to and from almost anything, so take the time to see what you want)

For cross-compiling fpc 3.1.1, all works, only the FreeBSD binary brandelf-Linux is needed to "brandelf" at end of compilation and a fix in t_bsd.pas (see attachment)
But I did not find a Linux-brandelf to use, maybe your link has it, I have to check.

Note that, for compiling fpc on a real FreeBSD aarch machine, /compiler/Makefile must be patched and add at each step of pre-compiler (pp1,pp2,..): (see attachment)
Code: Pascal  [Select][+][-]
  1. brandelf -t freebsd <ppX>
But there is the error when assembling system.ppu (see previous post).

Thanks.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018