AnyCPU
http://anycpu.org/forum/

Assembler in C: Programming Question
http://anycpu.org/forum/viewtopic.php?f=8&t=1086
Page 1 of 1

Author:  mmruzek [ Sat Nov 11, 2023 11:23 am ]
Post subject:  Assembler in C: Programming Question

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

Author:  robfinch [ Sat Nov 11, 2023 3:06 pm ]
Post subject:  Re: Assembler in C: Programming Question

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.

Author:  mmruzek [ Sat Nov 11, 2023 3:37 pm ]
Post subject:  Re: Assembler in C: Programming Question

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.

Attachments:
acer_710.jpg
acer_710.jpg [ 741.47 KiB | Viewed 622 times ]

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/