Author Topic: How to define a fixed length string padded with zeros  (Read 365 times)

Offline munair

  • Jr. Member
  • *
  • Posts: 4
  • Country: nl
  • keep it simple
    • SharpBASIC
How to define a fixed length string padded with zeros
« on: April 09, 2020, 03:28:36 PM »
While working on the SharpBASIC compiler I wonder how to define a null-terminated fixed length string.

Consider the following SharpBASIC code:

Code: [Select]
var buffer: string * 10 = $32;
This should be parsed into a NASM string with 10 spaces plus a null. The SharpBASIC compiler currently generates the following NASM code:

Code: [Select]
buffer times 10 db 32
          times 11-$+buffer db 0

Is this the correct output or is there a better syntax?
SharpBASIC (www.sharpbasic.com) is a compiler in development that uses NASM as backend.

Offline debs3759

  • Global Moderator
  • Jr. Member
  • *****
  • Posts: 92
  • Country: gb
    • GPUZoo
Re: How to define a fixed length string padded with zeros
« Reply #1 on: April 09, 2020, 04:18:14 PM »
I would use

Code: [Select]
buffer times 11 db 0

That will give you the 10 length string plus the zero terminator.
My graphics card database: www.gpuzoo.com

Offline munair

  • Jr. Member
  • *
  • Posts: 4
  • Country: nl
  • keep it simple
    • SharpBASIC
Re: How to define a fixed length string padded with zeros
« Reply #2 on: April 09, 2020, 04:48:06 PM »
I would use

Code: [Select]
buffer times 11 db 0

That will give you the 10 length string plus the zero terminator.

That was my initial solution as well. But the string may be predefined with characters like in the example above the space character (32), or something else like
Code: [Select]
var buffer: string * 10 = "abcde";
SharpBASIC (www.sharpbasic.com) is a compiler in development that uses NASM as backend.