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

  • Home
  • SEARCH
  • 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 6572965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:04:19+00:00 2026-05-25T15:04:19+00:00

So for my assignment I have to simulate basic machine language with C. The

  • 0

So for my assignment I have to simulate basic machine language with C. The machine has 16 registers (reg[]), a program counter(pc) and memory (mem[]) all of which are unsigned chars. The instructions are read in from a file and are in the format:

B404 (1RXY = Load register R with the value at memory address XY). All numbers are in hex. C000 is the halt command.

Now my problem is that when I print out the instructions (when stored in a,b,c,d and cd) b and d have leading zeros. How do I get rid of them so the instruction is printed as above? (Line 48).

Also it appears some of my if statements aren’t called as if I put a printf() in them and they are on the file it is never printed. (Lines 48 to 78).

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main () {
FILE *file;
file = fopen("a3.txt","r");
unsigned int temp = 0;
unsigned char pc, mem[256], reg[16],a,b,c,d,cd;
unsigned int count;
count = 0;
pc = 0;
for (int i=0; i < 16;++i) {
    reg[i] = 0;
}

if (file == NULL) {
    printf("\n Error opening the file");
    exit(0);
}
for (int i=0; !feof(file); i += 2) {
    fscanf(file, "%X", &temp);
    mem[i] = ((temp & 0xFF00) >> 8);
    mem[i + 1] = (temp & 0xFF);
    if (feof(file)) {
        break; // exit from while
    }
    ++count;
}
int fclose(FILE *file);
/*while (mem[pc] != 0xC0) {*/
for (unsigned int i=0; i < count-1;++i) {
    if (mem[i] == 0xC0) {
        break; // exit from for
        exit(1);
    }
    cd = mem[pc + 1];
    a = (mem[pc] & 0xF0);
    b = (mem[pc] & 0xF);
    c = (mem[pc + 1] & 0xF0);
    d = (mem[pc + 1] & 0xF);
    printf("%02X ",pc);
    printf("%X%X%X%X - [",a,b,c,d);
    printf("%02X %02X %02X %02X ",reg[0],reg[1],reg[2],reg[3]);
    printf("%02X %02X %02X %02X ",reg[4],reg[5],reg[6],reg[7]);
    printf("%02X %02X %02X %02X ",reg[8],reg[9],reg[10],reg[11]);
    printf("%02X %02X %02X %02X]\n",reg[12],reg[13],reg[14],reg[15]);
    if (a == 0x1) {
        reg[b] = reg[cd];
    }
    if (a == 0x2) {
        reg[b] = cd;
        //printf("2 reporting in");
    }
    if (a == 0x3) {
        reg[cd] = reg[b];
    }
    if (a == 0x4) {
        reg[d] = reg[c];
    }
    if (a == 0x05) {
        reg[d] = (reg[b] + reg[c]);
        //printf("5 reporting in");
    }
    if (a == 0x7) {
        reg[d] = (reg[b] | reg[c]);
    }
    if (a == 0x8) {
        reg[d] = (reg[b] & reg[c]);
    }
    if (a == 0x9) {
        reg[d] = (reg[b] ^ reg[c]);
    }
    if (a == 0xA0) {
        reg[b] = (reg[b] >> reg[d]) | (reg[b] << (32 - reg[d]));
    }
    pc += 2;
    if (a == 0xB0) {
        //printf("B reporting in");
        if (reg[b] == reg[0]) {
            pc = cd;
        }
    }
}
return 0;
}

Thanks!

  • 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-25T15:04:20+00:00Added an answer on May 25, 2026 at 3:04 pm

    To get the high bits of an unsigned char, use the following function:

    unsigned char hi(unsigned char bits) {
      return (bits >> 4) & 0x0F;
    }
    

    And when you are doing the instruction dispatch, it is better to use a switch statement instead of an if ... else if ... else cascade. Then you can have a simple default: fprintf(stderr, "unknown instruction: %u\n", a); exit(1); for error detection.

    Also, 0xA0 is not the value 10, its rather 160, so you should either write 10 or 0x0A.

    Update:

    The code for extracting a, b, c and d should look like this:

    a = (mem[pc + 0] >> 4) & 0x0F;
    b = (mem[pc + 0] >> 0) & 0x0F;
    c = (mem[pc + 1] >> 4) & 0x0F;
    d = (mem[pc + 1] >> 0) & 0x0F;
    

    This is simple to read and easy to check for correctness. One can clearly see the uniform handling, and that each value is restricted to the range [0x00;0x0F].

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

Sidebar

Related Questions

For my university assignment I have to design some basic managment system for sicknesses
I have an assignment in a language-independent class, and part of it is using
I have an assignment from my programming class, which is very poorly worded... The
So I have this assignment to simulate allocating data. It goes like this its
For a university assignment I have been assigned I have a Prize object which
I am software engineering student, and we have assignment to create a system, which
For a homework assignment I have to write a basic shell including redirection. The
This is one of the tasks of my assignment. I have a Turing machine
A recent homework assignment I have received asks us to take expressions which could
for an assignment I have to write a program that will take in an

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.