NASM - The Netwide Assembler

NASM Forum => Using NASM => Topic started by: Gunner on November 14, 2014, 03:58:45 AM

Title: Insert define/equ number into string var
Post by: Gunner on November 14, 2014, 03:58:45 AM
Let's say I have a string defined as such:
Code: [Select]
szErrInvalidEmailLen   db  "Email entered is too long, valid is less than 260 characters", 0
is there anyway to change the 260 to something like this:
Code: [Select]
szErrInvalidEmailLen   db  "Email entered is too long, valid is less than MAX_EMAIL_LEN characters", 0where MAX_EMAIL_LEN is defined as
Code: [Select]
MAX_EMAIL_LEN   equ 25
So, if I change MAX_EMAIL_LEN it will reflect in the string?
Title: Re: Insert define/equ number into string var
Post by: rkhb on November 14, 2014, 10:50:31 AM
My suggestion:

Code: [Select]
%defstr MAX_EMAIL_LEN 25
szErrInvalidEmailLen   db  "Email entered is too long, valid is less than ", MAX_EMAIL_LEN, " characters", 0
Title: Re: Insert define/equ number into string var
Post by: Rob Neff on November 14, 2014, 04:28:50 PM
My suggestion:

Code: [Select]
%defstr MAX_EMAIL_LEN 25
szErrInvalidEmailLen   db  "Email entered is too long, valid is less than ", MAX_EMAIL_LEN, " characters", 0

That suggestion is not going to work at all.  The defined value does not correlate to the proper ASCII value for display purposes.

What actually needs to occur is similar to the following C code:

Code: [Select]
#define MAX_EMAIL_LEN 25
char buf[260];
sprintf(buf, "Email entered is too long, valid is less than %d characters", MAX_EMAIL_LEN);
Title: Re: Insert define/equ number into string var
Post by: Gunner on November 15, 2014, 02:07:34 AM
Yes, I know about the printf family of functions; I was hoping for some preprocessor magic.
Title: Re: Insert define/equ number into string var
Post by: gammac on November 15, 2014, 12:27:11 PM
If you use a macro like this, you'll have to change only one value.

Code: [Select]
%macro equstr 1
%00 equ %1
%defstr %00_STR %1
%endmacro

MAX_EMAIL_LEN equstr 25

; now use MAX_EMAIL_LEN as normal equate and MAX_EMAIL_LEN_STR as a string representation

[SECTION .data]

szErrInvalidEmailLen db "Email entered is too long, valid is less than "
db MAX_EMAIL_LEN_STR
db " characters", 0
Title: Re: Insert define/equ number into string var
Post by: Gunner on November 16, 2014, 05:51:42 PM
Thanks, works perfectly!

Quote
That suggestion is not going to work at all.  The defined value does not correlate to the proper ASCII value for display purposes.
I just tried the suggestion from rkhb and it does work.  Dunno..
Title: Re: Insert define/equ number into string var
Post by: Rob Neff on November 17, 2014, 04:32:23 PM
You are correct.  I mistook the defstr for define.  However, gammac's suggestion is better as you get both a numeric and a string value to use throughout your source files.