Last visit was: Thu Oct 31, 2024 11:56 pm
It is currently Thu Oct 31, 2024 11:56 pm



 [ 3 posts ] 
 Assembler in C: Programming Question 
Author Message
User avatar

Joined: Sun Dec 19, 2021 1:36 pm
Posts: 87
Location: Michigan USA
Hi, I am writing an Assembler for my LALU computer in C. The first operation is simply a loop that compares 3 letter mnemonics "NOP", "NOT", "AND", etc and translates them to a hexadecimal opcode. I have a 3 element character array to hold the mnemonic, and I want to do a simple logical comparison to a character array constant. I can get this to work by comparing individual letters of the opcode. I can't seem to make this logical comparison more simple.

if(op[0]=='N' && op[1]=='O' && op[2]=='P'){
byte=0x00;fwrite(&byte,1,1,rom);}

I would have thought something like this would work

if(op == "NOP")

Wondering if anyone could suggest something to try. Thanks! Michael


Sat Nov 11, 2023 11:23 am WWW

Joined: Sat Feb 02, 2013 9:40 am
Posts: 2205
Location: Canada
C does not have built-in string handling. Strings are handled via library functions.

Quote:
if(op == "NOP")


Could be done as
Code:
if (strcmp(op,"NOP")==0)

Where strcmp is a library function that returns 0 if the strings are equal.

To use strcmp and other string functions, '#include <string.h>' at the top of the source code file.

You might want to setup a table of opcodes (an array of strings), and iterate through the table in a loop rather than using a whole lot of 'if (strcmp())' statements.

You may also want to try something like C# or VB which have string types. They are a little easier to work with than C.

Using something like the VASM assembler, which is written in C, may be desirable in the long run. The VASM code takes care of numerous details associated with an assembler, symbols, parsing expressions, etc. and may be used with VLINK to link code modules together.

_________________
Robert Finch http://www.finitron.ca


Sat Nov 11, 2023 3:06 pm WWW
User avatar

Joined: Sun Dec 19, 2021 1:36 pm
Posts: 87
Location: Michigan USA
Thanks Rob! Works great. My programming enviroment is an 8088 computer running PowerC, a compiler from the late 1980's. (My first x86 computer and my first compiler.) The computer really sounds like it is doing something when I compile the code! I put Mike Brutman's MTCP suite (http://brutmanlabs.org/mTCP/mTCP.html) on the machine so I can FTP files to-and-fro without using floppies. Works nice.


You do not have the required permissions to view the files attached to this post.


Sat Nov 11, 2023 3:37 pm WWW
 [ 3 posts ] 

Who is online

Users browsing this forum: claudebot and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software