views:

159

answers:

1

How do I put the value of 0x04 in register 4 if the instruction was 1rxy? 1RXY-Load register R with the value at memory address XY

#include <stdio.h>

unsigned char r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,ra,rb,rc,rd,re,rf;

void reg_check(unsigned char reg);
void rxy1(unsigned char reg, unsigned char val);

int main(){
    unsigned char memloc1=0x14;
    unisgned char memloc2=0x04;

    unsigned char temp,reg,val_add;
    temp=(x && 0xFF00) >> 8;

    if (temp = 0xB){
     reg=(memloc1 &0x0F);
     val_add=memloc2;
     rxy1(reg,val_add);
    }

    return 0;
}
void reg_check(unsigned char reg){

}
void rxy1(unsigned char reg, unsigned char val){

The actual instruction is 0x1404, this split into two bytes, memloc1 and memloc2. According to the format of 1rxy, which means to put value "at" memory location xy in register r.

so here register 4 or unsigned char r4 would have to hold the value at memory location 0x04 which would hold some other number.

My question is how do I test for the register variable by determining the "r" or 1"r"xy in 1"4"04 and placing the value held at the location xy into the unsigned char variable r4

for example if memory location 0x04 held 0xFB.

I hope this makes sense.

[edit] Example

#include <stdio.h>
int main(){
    unsigned char r0,r2,r3,r4;
    unsigned char mem1=0x14;  //at lmemory address 00
    unsigned char mem2=0x04;  //at lmemory address 01



    unsigned char reg_val_store=mem1 & 0x0F;


    if( ((mem1= & 0xF0) >> 4) == 0x1){
     if (reg_val_store == 0x4){
      //then put value store at memory address "04" into register 4.
      //and just say for example "0xFD" was at memory location "04"
      //since register value is 4 from the instruction read in 0x1"4"04

      //i want to put 0xFD in the r4 unsigned char variable, how do i do this?
      r4=0xFD; // this is of course correct but the instruction read in changes and 
             // so does the register variable. how do i modify my code for this change?
     }
    }

    return 0;
}
+1  A: 

If i understand correctly, you want to put B4 in memory[0] and 04 in memory[1]. Am i right?

This will do that.

memory[0] = ((x & 0xFF00) >> 8 ); //Will put B4 in memory[0]
memory[1] = (x & 0xFF); //Will put 04 in memory[1]

I think, next you want to check B and 4 seperately on memory[0] and then proceed to your next step. Right?

(memory[0] & 0xF0) >> 4 // will give you 0xB
(memory[0] & 0x0F) //will give you 0x4

Is this what you are looking for?

Update: For your reading problem, you should be using this .

while (!feof(f))
{
    fscanf(f,"%X",&inst[i]);
    i++;
}

This reads till EOF and you could use i value after this loop to know how many instructions are read and put it in a variable say n_instr. And then for looping thro' instructions you could use this

while(loop<n_instr) //instead of just loop<80
{
        memory[j] = ((inst[loop] & 0xFF00) >> 8 );
        j=j+2;
        memory[k] = (inst[loop] & 0x00FF);
        k=k+2;

        loop++;
}
Aviator
:) yes, Its because my instructions are to first read into an unsigned int then extract a byte into an unsigned char then follow on from there. I don't quite understand how to assign the last byte into the unsigned char and read values separately to determine what action/instruction to take.
sil3nt
edited my answer.. hope it helps
Aviator
It works. Thank you
sil3nt
Edited my answer again for your new reading problem
Aviator
sil3nt
I hope its not incorrect. Did you try it? fscanf returns number of succesful fields read, or EOF if it is encountered.
Aviator
I did try it, there are no errors, but all the values in memory changed..? this was why i switched to while(i<80) earlier.
sil3nt
Aviator
hmmm still doesn't work. this is odd
sil3nt
Edit your question and put the complete thing you are trying. there might be something else which is going wrong
Aviator
thank you for help aviator.
sil3nt
welcome. give the code in my answer a try. i think it wil work.
Aviator
Remember that feof returns true only after the first faild attempt to read from the file, so the "while (!feof(...))" pattern is (usually) wrong.
Thomas Padron-McCarthy
@thomas: when can we actually expect this to fail. Are these any specific circumstances? I have not used it extensively, but never experienced a problem with that. Could you explain a bit more?
Aviator
It works now, but in what situations are feof() wrong?. Also @aviator,(inst_count+1)*2 would be correct for any amount of instructions correct? (from my last update)
sil3nt
Put inst_count=i after the loop (while reading from file). You need not assign it for every iteration. ALso you are looping thro inst_count numbers only! So dont go for twice of that. It will not work. Just use the one from my code
Aviator
i.e> while (loop<inst_count)
Aviator
ohk but would i not need to add 1 to inst_count because i starts from a value of 0?
sil3nt
since you assign it outside the loop, i would have already been incremented which makes it unneccessary to add another 1 explicitly.
Aviator
hello again aviator, could you please take a look at my latest dilemma?, How do I put the value of 0x04 into register 4? and make it so that it adjusts for other instructions/values?utterly lost. Ive been flipping through pages of notes and been fiddling with pointers and i kept getting errors saying that i couldn't return pointer addresses through a char function..??please help.
sil3nt
i am also lost! what are you trying to achieve here? Load the instructions from a file and manipulate them with actual registers? or variables posing themselves as registers?
Aviator
Just variables posing themselves as registers aviator. This is just a simulation type thing that Im required to do for my class.
sil3nt
OKie.. got it now.. you are saying something about the values contained in memory location? Are these actuall addresses? I mean in real, they can hold any data? Are you having variables mimicking these addresses as well?
Aviator
I'm having variables mimicking these addresses as well so they're pretty much the same as saying- for example 0x1404 and in 0x14 is stored in memory location 01 and 0x04 is stored in memory location 02. So when it says put value at memory address 0x04 into register 4 it means put whatever at memory location 04 into register 4. These are all variables ofcourse.And the instructions are read in from a file like my previous code.
sil3nt
A simple one is to go for a switch for value of reg, and checking if its either 1 or 2 etc., and put it in proper register r1, r2 etc., For getting the value of memory 0x04, where are you stroing these values? In an array?
Aviator
I know how to check for which register but how do i put it in the correct register if I knew what register it ought to be in?
sil3nt
It is simple. if you are inside a case 4: of switch, you put the value inside r4 (similarly for all other registers. One case inside switch for each register). I hope you can get the value. Because i dont where are you storing the values.
Aviator
thanks a lot aviator!:)
sil3nt
welcome..did you make it wrk?
Aviator
yeahp got it to work, thanks.
sil3nt
great! gud luck!
Aviator