flat assembler
Message board for the users of flat assembler.

Index > Main > strict keyword like in NASM available?

Author
Thread Post new topic Reply to topic
LowLevelMahn



Joined: 30 Sep 2008
Posts: 12
LowLevelMahn 17 Aug 2018, 15:05
in nasm i can force the operand size - is somthing simliar possible with fasm?

Code:
; nasm nasm_sample.asm -fbin -o nasm_sample.com
org 100h
section .text
start:
add ax,3 ; ==> 83 C0 03
add ax,strict byte 3 ; ==> 83 C0 03
add ax,strict word 3 ; ==> 05 03 00
    
Post 17 Aug 2018, 15:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20292
Location: In your JS exploiting you and your system
revolution 17 Aug 2018, 15:07
Just omit the "strict" and you will get the sizes specified.
Code:
add ax,byte 3 ; ==> 83 C0 03
add ax,word 3 ; ==> 05 03 00    
Post 17 Aug 2018, 15:07
View user's profile Send private message Visit poster's website Reply with quote
LowLevelMahn



Joined: 30 Sep 2008
Posts: 12
LowLevelMahn 17 Aug 2018, 15:11
it does not work

Code:
;fasm fasm_dos_exe.asm
format MZ 
entry main:start
stack 100h
segment main
  start: 
  add ax,3 ; assembles
  add ax,byte 3 ; fails
    


this is the error i get

Code:
flat assembler  version 1.73.04  (1048576 kilobytes memory)
fasm_dos_exe.asm [8]:
  add ax,byte 3
processed: add ax,byte 3
error: operand sizes do not match.
    
Post 17 Aug 2018, 15:11
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 17 Aug 2018, 18:57
LowLevelMahn wrote:
it does not work

Code:
;fasm fasm_dos_exe.asm
format MZ 
entry main:start
stack 100h
segment main
  start: 
  add ax,3 ; assembles
  add ax,byte 3 ; fails
    


this is the error i get

Code:
flat assembler  version 1.73.04  (1048576 kilobytes memory)
fasm_dos_exe.asm [8]:
  add ax,byte 3
processed: add ax,byte 3
error: operand sizes do not match.
    

There’s no add reg16, imm8 in IA-32 ISA. I wonder why would any decent x86 assembler allow that.
Post 17 Aug 2018, 18:57
View user's profile Send private message Visit poster's website Reply with quote
LowLevelMahn



Joined: 30 Sep 2008
Posts: 12
LowLevelMahn 17 Aug 2018, 19:03
sorry its for IA-16 (DOS real mode)

https://c9x.me/x86/html/file_module_x86_id_5.html

...
05 iw, ADD AX, imm16, Add imm16 to AX
...
83 /0 ib, ADD r/m16, imm8, Add sign-extended imm8 to r/m16

nasm can assemble it using the strict keyword
add ax,strict byte 3 ; ==> 83 C0 03
add ax,strict word 3 ; ==> 05 03 00

its for an reverse engineering project where i first try to reproduce the very same executable (100% identical)
Post 17 Aug 2018, 19:03
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 17 Aug 2018, 19:41
LowLevelMahn wrote:
sorry its for IA-16 (DOS real mode)

https://c9x.me/x86/html/file_module_x86_id_5.html

...
05 iw, ADD AX, imm16, Add imm16 to AX
...
83 /0 ib, ADD r/m16, imm8, Add sign-extended imm8 to r/m16

nasm can assemble it using the strict keyword
add ax,strict byte 3 ; ==> 83 C0 03
add ax,strict word 3 ; ==> 05 03 00

its for an reverse engineering project where i first try to reproduce the very same executable (100% identical)

16 bits have been part of IA-32 for ages by now but I see your point. It actually is a sign-extension version and the operand is still 16-bit before addition. Since any immediate can be extended to the required size there’s no need to specify the size of an immediate if you write a program.

Your case is somewhat different: another compiler made a bad job choosing an encoding that is longer than the one FASM uses by default (since FASM generally chooses the shortest encoding possible). So, if your want to have a particular encoding, just do it the way you should have done it in any assembler: make it up by hand with db. The strict keyword you’re asking for is a very custom solution for a way more general problem. If you want to still have the instruction in human-readable form, you can write a macro. Something like (not tested):
Code:
macro add op1, op2
{
  regIdx = ... ; I have to think about a smarter way to calculate it
  if (op1 eqtype ax) & (op2 eqtype 0) & (op2 < $FF)
    db $83, $C0 or regIdx, op2
  else
    add op1, op2
  end if
}    

The way I can think off the top of my head is to define numeric constants for registers and use concatenation to build their names within macro. Although that’s too much work that would better be done by placing a comment next to db.
Post 17 Aug 2018, 19:41
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8349
Location: Kraków, Poland
Tomasz Grysztar 17 Aug 2018, 19:51
LowLevelMahn wrote:
this is the error i get

Code:
flat assembler  version 1.73.04  (1048576 kilobytes memory)
fasm_dos_exe.asm [8]:
  add ax,byte 3
processed: add ax,byte 3
error: operand sizes do not match.
    
This should be:
Code:
add ax,3      ; 83 C0 03 (automatically optimized to short imm whenever possible)
add ax,word 3 ; 05 03 00 (forced long immediate)    
You can look at some of my very old posts on the topic for an explanation of this syntactic choice.

This touches a general problem: how to allow to somehow influence the choice of encoding when a syntax is intended to be abstraction focused solely on what the instruction does. Ultimately this led me to the design of prototype fasm 2 syntax that is currently only implemented in form of experimental macros for fasmg.

You mentioned that your aim is to generate identical executable to the reverse engineered one. I would strongly suggest use of fasmg in place of fasm 1 for such task - fasm 1 leaves a footprint in the instruction encoding choices that cannot be easily altered, while with fasmg you can even modify macros to get the exactly encoding that you need (or perhaps x86-2.inc prototype in its current form might be enough).
Post 17 Aug 2018, 19:51
View user's profile Send private message Visit poster's website Reply with quote
LowLevelMahn



Joined: 30 Sep 2008
Posts: 12
LowLevelMahn 18 Aug 2018, 05:22
thanks - a little strage that byte isn't allowed but still works for my case
fasmg looks very promising
Post 18 Aug 2018, 05:22
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20292
Location: In your JS exploiting you and your system
revolution 18 Aug 2018, 06:23
LowLevelMahn wrote:
thanks - a little strage that byte isn't allowed but still works for my case
It is just a quirk of the instruction encoding. The value is sign extended internally to a full 16-bits before any arithmetic is performed. So logically the value is still considered to be 16-bits wide, but it can be stored in a compressed form of only 8-bits.
Post 18 Aug 2018, 06:23
View user's profile Send private message Visit poster's website Reply with quote
LowLevelMahn



Joined: 30 Sep 2008
Posts: 12
LowLevelMahn 18 Aug 2018, 14:07
Quote:
It is just a quirk of the instruction encoding


no i mean that fasm just don't accepts "add,byte 3" (because its already the default) and giving an operand size error

lets see how fasmg perform Smile
Post 18 Aug 2018, 14:07
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 1228
Location: Belarus
DimonSoft 18 Aug 2018, 17:48
LowLevelMahn wrote:
Quote:
It is just a quirk of the instruction encoding


no i mean that fasm just don't accepts "add,byte 3" (because its already the default) and giving an operand size error

lets see how fasmg perform Smile

FASM performs type checking (in a sense applicable to low-level programming) and since adding 16-bit to 8-bit is impossible with IA-32 (it’s not the same as instruction encoding with implied sign extension) the error message is what should be shown. Source code is for people, not processor, so it should better conform with program logic, not encoding logic. The same way it should complain in add ax, dword 3 case.
Post 18 Aug 2018, 17:48
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.