Author Topic: Forcing NASM to use certain code  (Read 13358 times)

Offline RuudB

  • Jr. Member
  • *
  • Posts: 40
  • Country: nl
    • Ruud's Commodore Site
Forcing NASM to use certain code
« on: April 28, 2014, 12:04:57 PM »
Hallo allemaal,

As already mentioned in another thread, http://forum.nasm.us/index.php?topic=1846.0, I wrote my own disassembler. So far it works great, I only need to fine tune 8087 opcodes. Assembling the result is something else. Assembling disassembled BINs that originally were created with NASM seems no problem so far. But when the BINs were created with other assemblers I ran into two problems:

- different code is used 
   For example: 'or al,al' is assembled by NASM to '08 C0'. But the original code is '0A C0'. The difference is the 'from/to' bit and the result in the end is exactly the same. But I wonder if there is a way to get the original code again.

- different length of code
   This is a problem. And I give you the most extreme example:
Original:   89 95 0000      mov   word ptr ds:[0][di],dx
NASM:   8915         mov   [di+00000h],dx

I understand why NASM does this; this code does the same but needs less bytes. My problem: I'm using NASM to recreate old BIOSes and now I find that after assembling the generated source I am missing several bytes. Which in this case means that, for example, the well known 'jmp 0F000h:coldboot' is not to be found at the usual address 0FFF0h anymore.

My main question: is there a way to force NASM to use the '+00000h' part as well?

Another example:
Original:   8B 90 0008      mov   dx,word ptr d_0007+1[bx+si]
NASM:   8B5008      mov   dx,[bx+si+00008h]

Any help or comment is welcome!


With kind regards / Met vriendelijke groet, Ruud Baltissen
www.Baltissen.org
With kind regards / met vriendelijke groet, Ruud Baltissen

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Forcing NASM to use certain code
« Reply #1 on: April 28, 2014, 01:27:51 PM »
Hallo Ruud,

Well...
Code: [Select]
mov [word di + 0], dx
mov dx. [bx + si + 800h]
... seems to get you a little closer. I'm not sure the second one's right. (the "word" specifier seems to need to be first in the brackets). I don't think there's a way to flip the "from/to" bit (there's the "db" option, but that's too ugly to contemplate).

Best,
Frank


Offline RuudB

  • Jr. Member
  • *
  • Posts: 40
  • Country: nl
    • Ruud's Commodore Site
Re: Forcing NASM to use certain code
« Reply #2 on: April 28, 2014, 07:15:33 PM »
Hallo Frank,

Code: [Select]
mov [word di + 0], dx
... seems to get you a little closer.
It did! I know that the use of 'word' and 'byte' is a must in some cases. But I hadn't seen any example like yours yet.

Thank you very much!


With kind regards / Met vriendelijke groet, Ruud Baltissen
www.Baltissen.org
With kind regards / met vriendelijke groet, Ruud Baltissen

Offline RuudB

  • Jr. Member
  • *
  • Posts: 40
  • Country: nl
    • Ruud's Commodore Site
Re: Forcing NASM to use certain code
« Reply #3 on: April 28, 2014, 07:54:30 PM »
Hallo Frank,

I updated my disassembler but at the end I ran into just one code I wasn't able to tackle:
Original:   81 E7 000E      and   di,0Eh
NASM:   83E70E      and   di,word 0000Eh

As you can see the 'word' has no effect here. I also tried 'and   word di,0000Eh' but with the same result. If you, or anyone else, have a trick in your sleeve that could help, I would be gratefull.


With kind regards / Met vriendelijke groet, Ruud Baltissen
www.Baltissen.org
With kind regards / met vriendelijke groet, Ruud Baltissen

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Forcing NASM to use certain code
« Reply #4 on: April 28, 2014, 09:01:15 PM »
Nasm is rather stubborn about doing the signed byte optimizations. In order to convince Nasm that you really really want the word form...
Code: [Select]
and di, strict word 0Eh
Someone explained to me once why it had to be that way, but I forget. I consider it a PITA, but I rarely need the "long form" anyway.

Oh, wait. To get 81 E7 00 0E, you'd want 0E00h which is a word anyway. In any case, the "strict" keyword may help you beat Nasm into submission.

Best,
Frank


Offline RuudB

  • Jr. Member
  • *
  • Posts: 40
  • Country: nl
    • Ruud's Commodore Site
Re: Forcing NASM to use certain code
« Reply #5 on: April 30, 2014, 12:51:49 PM »
Hallo Frank,

Quote
In any case, the "strict" keyword may help you beat Nasm into submission.
That is the trick, thank you !!!

Kind regards, Ruud
With kind regards / met vriendelijke groet, Ruud Baltissen