memorizzare un valore da un numero intero senza segno (lungo 2 byte) a una variabile char senza segno usando operatori bit per bit?

StackOverflow https://stackoverflow.com/questions/1414555

Domanda

Come inserisco il valore di 0x04 nel registro 4 se istruzione era 1rxy? 1RXY-Carica registro R con il valore all'indirizzo di memoria 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){

L'istruzione effettiva è 0x1404, suddivisa in due byte, memloc1 e memloc2. Secondo il formato di 1rxy, che significa mettere il valore "a" posizione di memoria xy nel registro r.

quindi qui il registro 4 o unsigned char r4 dovrebbe contenere il valore nella posizione di memoria 0x04 che contiene un altro numero.

La mia domanda è come testare la variabile di registro determinando la "quot" o 1 "r" xy in 1 "4" 04 e immissione del valore trattenuto nella posizione xy nella variabile char senza segno r4

ad esempio se la posizione di memoria 0x04 conteneva 0xFB .

Spero che abbia senso.

[modifica] Esempio

#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;
}
È stato utile?

Soluzione

Se ho capito bene, vuoi mettere B4 nella memoria [0] e 04 nella memoria [1]. Ho ragione?

Questo lo farà.

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

Penso che dopo si desideri controllare B e 4 separatamente nella memoria [0] e quindi procedere con il passaggio successivo. Giusto?

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

È questo quello che stai cercando?

Aggiorna : per il tuo problema di lettura, dovresti utilizzare questo.

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

Questo legge fino a EOF e potresti usare il valore i dopo questo ciclo per sapere quante istruzioni vengono lette e metterlo in una variabile dire n_instr. E poi per le istruzioni in loop puoi usare questo

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++;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top