1

First off, this is a homework assignment but I cannot for the life of me find any reliable resources online that explain using the syntax my professor has us using.

I wanted to be up front about this being homework so as not to get an answer straight up but to ask for an explanation so I can learn for future reference. My professor is not the best at providing resources for learning this stuff so I've decided to turn to the great people here at SO for assistance.

The assignment is fairly straight forward. Using an array of strings (the NATO Military Alphabet), receive a series of words from the user and then output that series of words to the screen in military alphabet.

So for example, the input "Feed" would be output as:

Foxtrot  
Echo  
Echo  
Delta

My question is quite basic. How does one declare a string array in Assembly (I'm fairly certain I'm using AT&T syntax since it using $ and % before variables and registers)?

Thanks for the help in advance! Let me know if I need to elaborate at all, I'll be happy to do so.

EDIT 1:

So with some explanations from the comments, I've come up with this so far for defining an array of addresses which point to asciz values for the military alphabet.

.section .data

Alpha:     .asciz "Alpha"
Beta:      .asciz "Beta"
Charlie:   .asciz "Charlie"
Delta:     .asciz "Delta"
Echo:      .asciz "Echo"
Foxtrot:   .asciz "Foxtrot"
Golf:      .asciz "Golf"
Hotel:     .asciz "Hotel"
India:     .asciz "India"
Juliet:    .asciz "Juliet"
Kilo:      .asciz "Kilo"
Lima:      .asciz "Lima"
Mike:      .asciz "Mike"
November:  .asciz "November"
Oscar:     .asciz "Oscar"
Papa:      .asciz "Papa"
Quebec:    .asciz "Quebec"
Romeo:     .asciz "Romeo"
Sierra:    .asciz "Sierra"
Tango:     .asciz "Tango"
Uniform:   .asciz "Uniform"
Victor:    .asciz "Victor"
Whiskey:   .asciz "Whiskey"
Xray:      .asciz "X-Ray"
Yankee:    .asciz "Yankee"
Zulu:      .asciz "Zulu"

MAlpha:    .long Alpha, Beta, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliet, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango, Uniform, Victor, Whiskey, Xray, Yankee, Zulu
Aphemra
  • 103
  • 7
  • 1
    @David C. Rankin: I don't think any assembler allows `dw "alpha"` to define a pointer to a string with that content elsewhere. You need `dw alphastring` and elsewhere `alphastring: db "alpha",0` (gas syntax probably uses different keywords). – ecm Oct 16 '20 at 18:11
  • 1
    @ecm Would you know the gnu gas equivalent for dw and db? I know I can use things like byte and long for numbers, but wasn't sure if I can use these for text? – Aphemra Oct 16 '20 at 18:26
  • 1
    I think what the problem wants is for you to create some kind of lookup table for the Alphabet as you iterate through the user input. One way to do that would be to use a bunch of `.string` directives with a label for each letter e.g. `alpha: .string "alpha"`. Then you can use an array of these labels, `.quad alpha, bravo, `, or `.long` depending on 32 or 64 bits. – General Grievance Oct 16 '20 at 18:30
  • @Calculuswhiz I'll definitely try this. My professor has used `asciz` in the single example of a text array I can find that he posted. Is this similar to `.string`? – Aphemra Oct 16 '20 at 18:34
  • 1
    @Aphemra Yup! In fact, I answered a question an this a while back! https://stackoverflow.com/questions/36854078/whats-the-difference-between-the-asciz-and-the-string-assembler-directives/59427479#59427479 – General Grievance Oct 16 '20 at 18:35
  • @Calculuswhiz I've edited the post with what I've come up with given your explanation. Yay or nay? – Aphemra Oct 16 '20 at 18:59
  • @Aphemra Looks good to me (haven't tested), except just `.data` instead of `.section .data`, I think. Your array is MAlpha now. Did you have additional questions beyond that? – General Grievance Oct 16 '20 at 19:06
  • @Calculuswhiz Nope, I think I can mark this closed/answered. Thanks for the assistance! – Aphemra Oct 16 '20 at 19:17
  • 1
    @ecm gcc actually defines `.globl arr` and creates a label for each string in `.rodata`, e.g. `.LC0:` and `.string "alpha"`. Then for the array definition uses `arr: long .LC0 ...` – David C. Rankin Oct 16 '20 at 21:47
  • 1
    @DavidC.Rankin: Your first comment is bogus: `dw` on x86 is a 16-bit word. And GAS doesn't use that directive. And you *definitely* don't want `dw "alpha", "bravo", ...` - that will pack the characters together with no `0` byte, except ones of odd length where padding each element of the comma-separated operand list to a whole number of words introduces a `0` ([that's if it's NASM](https://stackoverflow.com/questions/38860174/how-are-dw-and-dd-different-from-db-directives-for-strings). If MASM, it would probably error or reverse the order of bytes within words. GAS would error with `.long`) – Peter Cordes Oct 17 '20 at 10:12

1 Answers1

4

As shown in the edit of the post, using some help from the comments, I've answered this myself!

.section .data

Alpha:     .asciz "Alpha"
Beta:      .asciz "Beta"
Charlie:   .asciz "Charlie"
Delta:     .asciz "Delta"
Echo:      .asciz "Echo"
Foxtrot:   .asciz "Foxtrot"
Golf:      .asciz "Golf"
Hotel:     .asciz "Hotel"
India:     .asciz "India"
Juliet:    .asciz "Juliet"
Kilo:      .asciz "Kilo"
Lima:      .asciz "Lima"
Mike:      .asciz "Mike"
November:  .asciz "November"
Oscar:     .asciz "Oscar"
Papa:      .asciz "Papa"
Quebec:    .asciz "Quebec"
Romeo:     .asciz "Romeo"
Sierra:    .asciz "Sierra"
Tango:     .asciz "Tango"
Uniform:   .asciz "Uniform"
Victor:    .asciz "Victor"
Whiskey:   .asciz "Whiskey"
Xray:      .asciz "X-Ray"
Yankee:    .asciz "Yankee"
Zulu:      .asciz "Zulu"

MAlpha:    .long Alpha, Beta, Charlie, Delta, Echo, Foxtrot, Golf, Hotel, India, Juliet, Kilo, Lima, Mike, November, Oscar, Papa, Quebec, Romeo, Sierra, Tango, Uniform, Victor, Whiskey, Xray, Yankee, Zulu
Aphemra
  • 103
  • 7
  • 1
    You can put all of these in section `.rodata` instead of `.data` if you don't intend to modify them at runtime. – Nate Eldredge Oct 16 '20 at 21:09
  • 2
    FYI, the ITU / NATO phonetic alphabet uses "Bravo", not the greek letter "Beta". https://en.wikipedia.org/wiki/NATO_phonetic_alphabet – Peter Cordes Oct 17 '20 at 10:10