0

I am trying to make an assembler in C for the SIC architecture. And I saw that symbol table and object code table are usually made with hash tables.

What I wonder is, how assemblers print these tables.

  1. Do they memorize the insertion order of those hash tables?
  2. Or do they just print it by using intermmediate file?

And if it is the first case, how can I implement that in C?

I tried to search it in Internet, but it was no avail.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Dalim Oh
  • 1
  • 2
  • You mean for identifying tokens as keywords or instruction mnemonics, or for user-defined symbols finding the data structure where you store references to it? – Peter Cordes Nov 24 '22 at 11:10
  • @PeterCordes Oh, I'm so sorry. What I meant was how do they print hash tables. – Dalim Oh Nov 24 '22 at 11:13
  • Assemblers normally don't *print* hash tables at all, they use them internally but the output is just a binary (of machine code and data) or a text listing. Are you talking about assembling some SIC code that defines a hash table in SIC assembly language? Or maybe you're talking about adding some debug-print code to your assembler for use while developing it? In that case up to you, but normally you wouldn't record the insertion order. You might sort it by something, or traverse it in hash order. – Peter Cordes Nov 24 '22 at 11:17
  • @PeterCordes Oh, no. What I wanted know is literally how can they print hash tables. I was required to make sic assembler in c and then print the contents of symbol tables. So I just figured sic-assemblers could do that, cause this whole assembler concept is new to me. (first time I see that) I'm sorry for causing confusion. – Dalim Oh Nov 24 '22 at 11:24
  • Ok, so yes, you are asking for a debug-print of the assembler's internal data structures. That's not necessary for actually assembling, but could be useful for debugging. Or in this case for someone looking at your homework to check the symbol table. Normally you'd just loop over the array entries and for any that are valid, print the key and the value. – Peter Cordes Nov 24 '22 at 11:31
  • @PeterCordes Well, actually that is where the problem comes from. I submitted a design plan(if that is how they call) where I mentioned I would implement symbol table with hash table. So I asked the instructor that If I just can print it without order. Then I got rejected immediately, and was said that I have to follow the plan I made. So now, I have to print it "in order" with hash table. I have no idea how to do that, though. – Dalim Oh Nov 24 '22 at 11:38
  • Oh, maybe they want you to print it in order of increasing address, like the [Unix `nm` tool](https://man7.org/linux/man-pages/man1/nm.1.html) does when you run it on an executable or object file. See [How do I list the symbols in a .so file](https://stackoverflow.com/q/34732) for some example outputs. So you'll need to get a list of keys, sort them by address, then loop over that sorted collection. Or I guess get a list of address/name pairs and sort those structs, so you don't have to access the hash table while sorting or printing, especially since you're sorting on values not hash keys – Peter Cordes Nov 24 '22 at 11:51
  • 1
    @PeterCordes I think you are right! Making list by using address will definitely work! Thank you so much, peter! – Dalim Oh Nov 24 '22 at 12:03

0 Answers0