Recent

Author Topic: Assembly code branching  (Read 3525 times)

DougRaymond

  • Newbie
  • Posts: 3
Assembly code branching
« on: July 02, 2019, 09:37:54 am »
I have been trying for days to work out how to do conditional jumps, but everything I try gives an error. Can someone tell me how to do it? a sample of the code is in the attached ASM.jpg partial screen print of the editor. Thankyou.

madref

  • Hero Member
  • *****
  • Posts: 1116
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Assembly code branching
« Reply #1 on: July 02, 2019, 09:56:05 am »
This is a lazarus forum....not an assembler forum :)
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Tahoe 26.2
Lazarus 4.99 (rev main_4_99-3149-g7867f6275c) FPC 3.3.1 x86_64-darwin-cocoa

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12718
  • FPC developer.
Re: Assembly code branching
« Reply #2 on: July 02, 2019, 09:56:55 am »
Did you enable {$asmmode intel} ?

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Assembly code branching
« Reply #3 on: July 02, 2019, 11:54:22 am »
Declare labels in a Label section.

BeanzMaster

  • Sr. Member
  • ****
  • Posts: 268
Re: Assembly code branching
« Reply #4 on: July 02, 2019, 06:02:20 pm »
Hi just use @ for label

Ex @Loop :
Why don't you use Push and pop for save and restore register ?

440bx

  • Hero Member
  • *****
  • Posts: 6154
Re: Assembly code branching
« Reply #5 on: July 02, 2019, 06:50:07 pm »
Doug,

At first sight, I don't think that assembly code is going to be significantly faster than something you would write in plain Pascal (particularly once optimizations are set "high" (3))

Did you try writing it in Pascal ? 

Once you have it working in assembly, you might want to test it against a pure Pascal implementation.  The performance gain may not be worth using Assembler.

HTH.


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

DougRaymond

  • Newbie
  • Posts: 3
Re: Assembly code branching
« Reply #6 on: July 03, 2019, 08:18:09 am »
Thankyou to all the people that responded to my question. I will reply in order.
madref - Lazarus allows inline assembly code.
marcov - This actually worked. Thankyou. Guess I will never know why my branches in AT&T assembly code would not work, but at least they work in Intel assembly code.
engkin – I don’t know how to do that.
BeanzMaster – That is how it is done in Intel assembly code. It does not work in AT&T. I don’t know why or when I switched from pushing and popping. You are right. It is a better way.
440bx – You are right. There is no particular advantage in writing this procedure in assembly code.

Extra information. I wrote this application in Delphi 5 for Windows 98 last century. It allows scrolling text to be displayed via a data projector in a church or karaoke situation. I am amazed that it still works on Windows 10. Recently people have been nagging me to produce a 64 bit version. I was hoping that I could do this through Lazarus.
My application is mostly written in Pascal. The only bits that require assembly code are the bits that combine the words and background bitmaps to produce the scroll.
If I can get the original to work in Lazarus it will save me a lot of time against starting from scratch again in cpp, which is the only other language that I know that allows inline assembly code.



marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12718
  • FPC developer.
Re: Assembly code branching
« Reply #7 on: July 03, 2019, 08:33:17 am »

marcov - This actually worked. Thankyou. Guess I will never know why my branches in AT&T assembly code would not work, but at least they work in Intel assembly code.

Labels start with a dot in AT&T. 

Assembly code usually needs some updates when changing to 64-bit (it is a different architecture, even though related). Not always hard, but requires knowledge which makes it non trivial.
 

440bx

  • Hero Member
  • *****
  • Posts: 6154
Re: Assembly code branching
« Reply #8 on: July 03, 2019, 01:11:57 pm »
... starting from scratch again in cpp, which is the only other language that I know that allows inline assembly code.
Be aware and beware that not all 64bit cpp compilers allow inline assembly,  MSVC among them.

My suggestion, whichever way you end up implementing the application, don't use assembly unless there really is an overriding reason for it.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12718
  • FPC developer.
Re: Assembly code branching
« Reply #9 on: July 03, 2019, 01:31:29 pm »
Afaik delphi doesn't either.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6355
  • Compiler Developer
Re: Assembly code branching
« Reply #10 on: July 03, 2019, 01:42:33 pm »
Afaik delphi doesn't either.
You need to have the whole function body as assembly. Inline assembly blocks are not supported.

Pascal

  • Hero Member
  • *****
  • Posts: 932
Re: Assembly code branching
« Reply #11 on: July 03, 2019, 03:39:17 pm »
My application is mostly written in Pascal. The only bits that require assembly code are the bits that combine the words and background bitmaps to produce the scroll.

I am just playing around with a similar app. Download preview project at
https://www.dropbox.com/sh/lqx33xx38p3c3b0/AADbP3ikJJpwPPTO4TdMRcGra?dl=0
You have to install freetype.dll.

laz trunk x64 - fpc trunk i386 (cross x64) - Windows 10 Pro x64 (21H2)

MathMan

  • Sr. Member
  • ****
  • Posts: 473
Re: Assembly code branching
« Reply #12 on: July 04, 2019, 08:51:38 am »
Hi Doug,

Welcome to the forum btw. I am in line with 440bx regarding the use of assembly. But first of all have a look at the algorithm. From your source it looks like you want to do a "saturated add" on a byte level. You are doing that via compare and branch, which generates data dependent branch mispredictions that will heavily slow down modern processors. A 1:1 translation of this to Pascal will have the same issues. And the added downside of this is that the execution speed of your routine is depending on the image you handle!

With some bit-wizardry you can make that code branch free if you considering the following

 $xx + $yy = carry-bit + $zz => if you do bitwise or of $zz with Carry*255 you get what you want

The above is branch free and probably the Pascal version is already faster than your current asm, even if you handle one byte after the other. However, the branch free variant can be modified to handle all four bytes of a DWORD in parallel (or on a 64 bit machine even 8 bytes of a QWORD) giving a substantial boost.

If you are really craving for speed and willing to restrict to certain processor types, then there are AVX instructions on modern x86-64 processors that handle "saturated adds" and can provide another substantial boost. However I would first start with a Pascal version of the branch-free approach I explained above.

Regards,
MathMan

DougRaymond

  • Newbie
  • Posts: 3
Re: Assembly code branching
« Reply #13 on: July 04, 2019, 01:00:09 pm »
This has been a most interesting experience. Some of my old mates who love an old application that I wrote, wanted me to upgrade it to 64 bit. I thought Lazarus may be a an easy solution. Unfortunately it isn’t.

I asked for someone to assist me with the syntax of branches. The dot doesn’t work in AT&T either.

I did not ask for an evaluation on my style of programming. I posted a very simple example, that allows the user to make a picture brighter. Whether it takes a nano- second to run or five-seconds is of no importance.

The standard version of Delphi 5 handles in line assembler like a breeze. So does the Visual Studio 2019 C++. I have used both.

For your added amusement, I have added as an attachment, the central procedure which combines the text bitmap with a background picture bitmap and writes it directly to the backbuffer of the graphics card. You can have fun picking that to pieces. Meanwhile I shall return to Visual Studio which actually works


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12718
  • FPC developer.
Re: Assembly code branching
« Reply #14 on: July 04, 2019, 01:06:00 pm »
First, don't use images, this makes a quick test too hard.

Second, such things work fine in 32-bit mode in Lazarus too, if you set the right modes.

Lazarus is simply more flexible in that it accepts assembler in more styles. (most notably Microsoft-intel-borland (Intel style) and Unix (AT&T style though after 2010 Intel style is getting more popular there)

Freepascal used an open source assembler as backend that in the early days exclusively supported AT&T style. Which is why it got to be the default.

 

TinyPortal © 2005-2018