News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Masm clones: Interesting error 'Symbol redefinition'

Started by _japheth, April 03, 2022, 09:41:29 PM

Previous topic - Next topic

_japheth


Here's an interesting piece of code that makes the wasm derivates emit the strange error msg "symbol redefinition: numfifields".


.model tiny

;--- define a string in .const
CStr macro text:vararg
local sym
    .const
sym db text,0
    .code
    exitm <offset sym>
endm

.data

fifields label word
dw CStr('Total Free Clusters: ')
dw CStr('First Free Cluster: ')
numfifields equ ($ - offset fifields) / sizeof word

.code

start:
    mov di, offset fifields
    mov cx, numfifields
nextitem:
;--- do something here
loop nextitem
    ret

END start



The code is 16-bit, but this is irrelevant here, you may change it to 32/64/... bit if you like.

Little contest: can you see the error, just by looking at the code and not feeding masm with it?  :bgrin:
Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

HSE

Quote from: _japheth on April 03, 2022, 09:41:29 PM
Little contest: can you see the error, just by looking at the code and not feeding masm with it?  :bgrin:
Segment declarations in Tiny Model. Perhaps are ignored, I don't tested yet :biggrin: (also you have to jump data I think)
Equations in Assembly: SmplMath

Biterider

Hi Japheth
I suspected a problem and verified it with UASM. I found it too, but I think I disqualified myself  :sad:

Biterider

Greenhorn

I'm not that super-geek, but is it because location counter $ is set to zero when entering a segment ?
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

_japheth

Quote from: HSE on April 04, 2022, 12:07:56 AM
Segment declarations in Tiny Model. Perhaps are ignored, I don't tested yet :biggrin: (also you have to jump data I think)

Bitness and memory model are irrelevant for this error - it also would happen for .model flat. It's 16-bit just because I stumbled upon that "bug" while modifying such code.
Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

avcaballero

Here is when one shows how little knows about programming  :toothy:, but what the hell, I like contests, maybe changing ".const" by ".data"?

jj2007

offset fifields is in the .data segment, while $ (when using the equate) is in the .code segment. Btw ml.exe also doesn't like it.

HSE

Quote from: jj2007 on April 04, 2022, 06:51:14 AM
offset fifields is in the .data segment, while $ (when using the equate) is in the .code segment. Btw ml.exe also doesn't like it.

You don't have segments in Real Mode  :biggrin: :biggrin: (or all the same  :thumbsup:)
Equations in Assembly: SmplMath

jj2007

Test it...

include \masm32\include\masm32rt.inc

.data
fifields label word
dw 123
dw 123
.code

numfifields1 equ $ - offset fifields ; line 9
numfifields2 = $ - offset fifields
numfifields3 equ <$ - offset fifields>




start:
  mov eax, numfifields1 ; line 17
  mov eax, numfifields2
  mov eax, numfifields3
  print str$(eax)
  exit

end start


ML:
tmp_file.asm(10) : error A2025:operands must be in same segment
tmp_file.asm(17) : error A2025:operands must be in same segment
tmp_file.asm(18) : error A2006:undefined symbol : numfifields2
tmp_file.asm(19) : error A2025:operands must be in same segment

UAsm:
Tmp_File.asm(9) : Error A2143: Symbol redefinition: numfifields1
Tmp_File.asm(10) : Error A2193: Operands must be in same segment
Tmp_File.asm(19) : Error A2193: Operands must be in same segment

_japheth


So in case you didn't find the error: it's a mistake in the macro, where the last line should be ".data" instead of ".code".

It's an error for all assemblers, of course - the point is that the error msg of the wasm derivates is misleading - for "newbies" it might be impossible to find out what's wrong.

Background: the difference of two relocatable symbols is a constant value - if both symbols are in the same segment. If not, the difference can only be calculated during the link step.
The problem of jwasm, uasm, asmc is that in the case of EQU, they generally assume - during pass 1 - that the difference is a valid constant. In pass 2, they "suddenly" realize that the difference is NOT a constant and hence the EQU symbol is to be a text macro.
And since a EQU constant cannot be redefined to a text macro, the assemblers emit this strange error message - it's surely a bug.
Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

jj2007

Right. The code below works fine, but in the moment when you move the equate under .code, it will fail.

include \masm32\include\masm32rt.inc

.data?
fifields label word
dw 100 dup(?)
numfifields = $ - offset fifields

.code
start:
  print str$(numfifields), " is the size of fifields"
  exit

end start