Author Topic: Insert define/equ number into string var  (Read 7837 times)

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Insert define/equ number into string var
« 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?

Offline rkhb

  • Jr. Member
  • *
  • Posts: 7
  • Country: de
Re: Insert define/equ number into string var
« Reply #1 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

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Insert define/equ number into string var
« Reply #2 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);
« Last Edit: November 14, 2014, 04:40:11 PM by Rob Neff »

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Re: Insert define/equ number into string var
« Reply #3 on: November 15, 2014, 02:07:34 AM »
Yes, I know about the printf family of functions; I was hoping for some preprocessor magic.

Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: Insert define/equ number into string var
« Reply #4 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
« Last Edit: November 15, 2014, 12:49:18 PM by gammac »
Please comment your code! It helps to help you.

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Re: Insert define/equ number into string var
« Reply #5 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..
« Last Edit: November 17, 2014, 03:03:09 AM by Gunner »

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 429
  • Country: us
Re: Insert define/equ number into string var
« Reply #6 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.