Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:41:38+00:00 2026-05-11T16:41:38+00:00

I’m writing an assembler for a custom micro controller I’m working on. I’ve got

  • 0

I’m writing an assembler for a custom micro controller I’m working on. I’ve got the assembler to a point where it will assemble instructions down to binary.

However, I’m now having problems with getting labels to work. Currently, when my assembler encounters a new label, it stores the name of the label and the memory location its referring to. When an instruction references a label, the assembler looks up the label and replaces the label with the appropriate value.

This is fine and dandy, but what if the label is defined after the instruction referencing it? Because of this, I need to have my parser run over the code twice.

Here’s what I currently have for my main function:

303 int main(int argc, char* argv[])
304 {
305 
306     if(argc < 1 || strcmp(argv[1],"-h")==0 || 0==strcmp(argv[1],"--help"))
307     {
308         //printf("%s\n", usage);
309         return 1;
310     }
311     // redirect stdin to the file pointer
312     int stdin = dup(0);
313     close(0);
314 
315     // pass 1 on the file
316     int fp = open(argv[1], O_RDONLY, "r");
317     dup2(fp, 0);
318 
319     yyparse();
320 
321     lseek(fp, SEEK_SET, 0);
322 
323     // pass 2 on the file
324     if(secondPassNeeded)
325     {
326         fp = open(argv[1], O_RDONLY, "r");
327         dup2(fp, 0);
328         yyparse();
329     }
330     close(fp);
331 
332     // restore stdin
333     dup2(0, stdin);
334 
335     for(int i = 0; i < labels.size(); i++)
336     {
337         printf("Label: %s, Loc: %d\n", labels[i].name.c_str(), labels[i].memoryLoc);
338     }
339     return 0;
340 }

I’m using this inside a flex/bison configuration.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:41:38+00:00Added an answer on May 11, 2026 at 4:41 pm

    If that is all you need, you don’t need a full two-pass assembler. If the label is not defined when you reference it, you simply output a stand-in address (say 0x0000) and have a data structure that lists all of the places with forward references and what symbol they refered to. At the end of the file (or block if you have local symbols), you simply go through that list and patch the addresses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 157k
  • Answers 157k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See Math.Pow. The function takes a value and raises it… May 12, 2026 at 11:05 am
  • Editorial Team
    Editorial Team added an answer -[NSManagedObject entity] returns the NSEntityDescription. This then lets you find… May 12, 2026 at 11:05 am
  • Editorial Team
    Editorial Team added an answer SO: Pass vars to JavaScript via the SRC attribute *Edit:… May 12, 2026 at 11:05 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.