News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

What is $-msg

Started by Shintaro, November 18, 2022, 08:14:35 PM

Previous topic - Next topic

Shintaro

Hi,
Hopefully, I don't embarrass myself too much, what the heck is "$-msg". Is it a macro or something?
P.28 Advanced MSDOS Programming.

I searched through Ray Duncans Book but can't find anything. Is it some inbuild way to calculate the length of the string?



Using MASM 5.1


stdin equ 0
stdout equ 1
tderr equ 2

cr equ 0dh
lf equ 0ah
_TEXT segment word public 'CODE'


org 100h

assume cs:_TEXT,ds:_TEXT,es:_TEXT,ss:_TEXT
print proc near

mov ah,40h
mov bx,stdout
mov cx,msg_len
mov dx,offset msg
int 21h

mov ax,4c00h
int 21h

print endp

msg db cr,lf

msg_len equ $-msg     ;<--------HERE


_TEXT ends

end print
"Wyrd bið ful āræd. Fate is inexorable."

Biterider

Hi Shintaro
This line calculates the byte length of "msg" as the current position ($) on the selected section minus the starting address of "msg".

Biterider

Shintaro

Is there a way I can see how this is working?
Because it is printing other garbage on the screen.
"Wyrd bið ful āræd. Fate is inexorable."

mineiro

dollar sign means exact position, it's used when we need know sizeof data or code.
I used uasm assembler and dosbox in linux to test
uasm -bin test.asm
ren test.bin test.com
dosbox ./
test.com



stdin equ 0
stdout equ 1
tderr equ 2

cr equ 0dh
lf equ 0ah
_TEXT segment word public 'CODE'


org 100h

assume cs:_TEXT,ds:_TEXT,es:_TEXT,ss:_TEXT
print proc near

mov ah,40h
mov bx,stdout
mov cx,msg_len
mov dx,offset msg
int 21h

mov ah,40h
mov bx,stdout
mov cx,nop_size
mov dx,offset nope
int 21h


mov ax,4c00h
int 21h

print endp

msg db "hello world",cr,lf
msg_len equ $-msg     ;<--------HERE


nop_label:
nop
nop_size equ $-nop_label
nope db nop_size+"0",cr,lf     ;<---| To convert size to be printable I add a "0"


one db 1
two db 1
three db 1
sizeof_one_to_three equ $-one

_TEXT ends

end print

I'd rather be this ambulant metamorphosis than to have that old opinion about everything

mineiro

You can calculate that by using 2 labels too:

code_start:                  ;code size
;program code instructions
code_end:

data_start:
;data types
data_end:

code_size equ code_end - code_start
data_size equ data_end - data_start
program_size equ code_size + data_size

PS:SHINTARO, edited after;
When I copied your source code from this board and pasted in a text editor, I perceived some strange chars in your source code. (one example is one byte before "org 100h").
Try using other text editor, your code it's working fine!!!.
Not sure how you're doing this, by ms-dos or windows. Copy my source code into notepad, save in target directory (folder) and should work too.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Shintaro

Ugh!
Sorry I missed a line of code. I really do need to wait until my kids are in bed before I post.


Attached is the garbage after the message display. It must be calculating the size incorrectly.



stdin equ 0
stdout equ 1
tderr equ 2

cr equ 0dh
lf equ 0ah
_TEXT segment word public 'CODE'


org 100h

assume cs:_TEXT,ds:_TEXT,es:_TEXT,ss:_TEXT
print proc near

mov ah,40h
mov bx,stdout
mov cx,msg_len
mov dx,offset msg
int 21h

mov ax,4c00h
int 21h

print endp

msg db cr,lf
    db 'Hello world!',cr,lf ; <-----line added, sorry.


msg_len equ $-msg     ;<--------HERE


_TEXT ends

end print

"Wyrd bið ful āræd. Fate is inexorable."

mineiro

When I copy and paste your source code I can see some "not allowed" chars in text.
This is the hex dump of your last source code:
I marked some of them with bold, but exist more, maybe the error it's in your text editor thats inserting these chars (C2h A0h) that I'm seeing as being "spaces".
When I remove all that (C2h A0h) I was able to assembler your source code.

00000000 73 74 64 69 │ 6E 20 65 71 │ 75 20 30 0D │ 0A 73 74 64 │ 6F 75 74 20 │ 65 71 75 20 │ 31 0D 0A 74 │ 64 65 72 72  stdin equ 0..stdout equ 1..tderr
00000020 20 65 71 75 │ 20 32 0D 0A │ 0D 0A 63 72 │ 20 65 71 75 │ 20 30 64 68 │ 0D 0A 6C 66 │ 20 65 71 75 │ 20 30 61 68   equ 2....cr equ 0dh..lf equ 0ah
00000040 0D 0A 5F 54 │ 45 58 54 20 │ 73 65 67 6D │ 65 6E 74 20 │ 77 6F 72 64 │ 20 70 75 62 │ 6C 69 63 20 │ 27 43 4F 44  .._TEXT segment word public 'COD
00000060 45 27 0D 0A │ 0D 0A 0D 0A │ C2 A0 6F 72 │ 67 20 31 30 │ 30 68 0D 0A │ 0D 0A C2 A0 │ 61 73 73 75 │ 6D 65 20 63  E'......  org 100h....  assume c
00000080 73 3A 5F 54 │ 45 58 54 2C │ 64 73 3A 5F │ 54 45 58 54 │ 2C 65 73 3A │ 5F 54 45 58 │ 54 2C 73 73 │ 3A 5F 54 45  s:_TEXT,ds:_TEXT,es:_TEXT,ss:_TE
000000A0 58 54 0D 0A │ 70 72 69 6E │ 74 20 70 72 │ 6F 63 20 6E │ 65 61 72 0D │ 0A 0D 0A C2 │ A0 6D 6F 76 │ 20 61 68 2C  XT..print proc near....  mov ah,
000000C0 34 30 68 0D │ 0A C2 A0 6D │ 6F 76 20 62 │ 78 2C 73 74 │ 64 6F 75 74 │ 0D 0A C2 A0 │ 6D 6F 76 20 │ 63 78 2C 6D  40h..  mov bx,stdout..  mov cx,m
000000E0 73 67 5F 6C │ 65 6E 0D 0A │ 6D 6F 76 20 │ 64 78 2C 6F │ 66 66 73 65 │ 74 20 6D 73 │ 67 0D 0A C2 │ A0 69 6E 74  sg_len..mov dx,offset msg..  int
00000100 20 32 31 68 │ 0D 0A 0D 0A │ C2 A0 6D 6F │ 76 20 61 78 │ 2C 34 63 30 │ 30 68 0D 0A │ C2 A0 69 6E │ 74 20 32 31   21h....  mov ax,4c00h..  int 21
00000120 68 0D 0A 0D │ 0A 70 72 69 │ 6E 74 20 65 │ 6E 64 70 0D │ 0A 0D 0A 6D │ 73 67 20 64 │ 62 20 63 72 │ 2C 6C 66 0D  h....print endp....msg db cr,lf.
00000140 0A 20 20 20 │ 20 64 62 20 │ 27 48 65 6C │ 6C 6F 20 77 │ 6F 72 6C 64 │ 21 27 2C 63 │ 72 2C 6C 66 │ 20 3B 20 3C  .    db 'Hello world!',cr,lf ; <
00000160 2D 2D 2D 2D │ 2D 6C 69 6E │ 65 20 61 64 │ 64 65 64 2C │ 20 73 6F 72 │ 72 79 2E 0D │ 0A 0D 0A 0D │ 0A 6D 73 67  -----line added, sorry.......msg
00000180 5F 6C 65 6E │ 20 65 71 75 │ 20 24 2D 6D │ 73 67 20 20 │ 20 20 20 3B │ 3C 2D 2D 2D │ 2D 2D 2D 2D │ 2D 48 45 52  _len equ $-msg     ;<--------HER
000001A0 45 0D 0A 0D │ 0A 0D 0A 5F │ 54 45 58 54 │ 20 65 6E 64 │ 73 0D 0A 0D │ 0A C2 A0 65 │ 6E 64 20 70 │ 72 69 6E 74  E......_TEXT ends....  end print
000001C0 0D 0A


Are you able to assemble attached source code and run it?
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Shintaro

Attached is the asm from the VM running DOS 6.22, written on ms edit.
It must be a formatting issue, your test.asm compiles and runs as expected.
I seem to recall something about alignment with MASM 5.x

"Wyrd bið ful āræd. Fate is inexorable."

mineiro

Your source code runs as expected in my machine.
I can see that you forgot to set CX register with sizeof string. Your attached source code does not contain that "not allowed" chars. :thumbsup:


...
        mov     ah,40h
        mov     bx,stdout
        mov     cx,msg_len          ;<----------|
        mov     dx,offset msg
        int     21h
...
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

Shintaro

Quote from: mineiro on November 19, 2022, 12:30:28 AM
Your source code runs as expected in my machine.
I can see that you forgot to set CX register with sizeof string. Your attached source code does not contain that "not allowed" chars. :thumbsup:


...
        mov     ah,40h
        mov     bx,stdout
        mov     cx,msg_len          ;<----------|
        mov     dx,offset msg
        int     21h
...

Bugger! Yea, that was it.
Something simple.
I was looking at all kinds of complex issues, calculations of bytes, alignment of source in the editor... But all it was, was a line of code.
Thanks mate.
"Wyrd bið ful āræd. Fate is inexorable."

jj2007

CodeSize MACRO algo
LOCAL csDiff, tmp$
csDiff=$-algo
tmp$ CATSTR <** &algo is >, %csDiff, < bytes long **>
% echo tmp$
ENDM


Usage:
xxx:
  mov eax, 123
CodeSize xxx
.err

Shintaro

Quote from: jj2007 on November 19, 2022, 01:47:40 AM
CodeSize MACRO algo
LOCAL csDiff, tmp$
   csDiff=$-algo
   tmp$ CATSTR <** &algo is >, %csDiff, < bytes long **>
   % echo tmp$
ENDM


Usage:
xxx:
  mov eax, 123
CodeSize xxx
.err

Yea, never really liked BASIC. Even on the Commodore Vic 20, in 1985, I was playing around with assembly.
I even had friends after University offer me jobs coding in basic, I just never liked it.
"Wyrd bið ful āræd. Fate is inexorable."

jj2007

Where do you see any BASIC in the snippet I posted, Shintaro?

Shintaro

Quote from: jj2007 on November 19, 2022, 10:46:48 AM
Where do you see any BASIC in the snippet I posted, Shintaro?
Ok, "Removing foot from mouth". :rolleyes:
Yea, I'm on a roll of screwing things up.
It might be time to go back to bed and wait until tomorrow.
"Wyrd bið ful āræd. Fate is inexorable."

jj2007

Actually, the version I posted above was far too complicated:

include \masm32\include\masm32rt.inc
CodeSize MACRO algo
  % echo @CatStr(<** &algo is >, %($-algo), < bytes long **>)
ENDM

.code
start:
_mov:
  mov eax, 123
  CodeSize _mov

_m2m:
  m2m eax, 123
  CodeSize _m2m
  .err ; prevent building (optional)
  exit
end start


Output:
** _mov is 5 bytes long **
** _m2m is 3 bytes long **