CPU Simulator geschlossen 

Status
Für weitere Antworten geschlossen.

kebapmaster

Registered +
Registriert
Apr. 2008
Beiträge
20
Hallo leute,
Bräuchte bei prog eure hilfe. Muss ein CPU simultator programmieren weiss nicht wie ich das angehen soll! hoffe ihr könnt mir da weiterhelfen!!

wir haben eine text datei vorgegeben und dazu muss ich ein prog.schreiben das diese punkte erfüllt:


2)replace all constants with their values and remove the definition lines containing the constants,

3)map the commands to address locations evaluating and replacing the .org commands (the entire address range is linear covering 2^9 address locations),

4)replace the labels within the code with addresses and replace the variable names with their locations that were assigned in the previous step.

5)Finally write the initial values of the variables to the according locations.

Code:
; a simple program that adds two constants using a sub-routine and stores 
; the result in a variable vc 
; 
ca .equ 5 ; define a constant ca with value 5 
cb .equ 7 ; define a constant cb with value 7 

.org 128 
vc .dw 0 ; define a variable vc at address 128 (= 0x80) with value 0 

.org 0 
_main: 
lda,c ca ; load constant ca to accu 
push,r a ; push accu to the stack 
lda,c cb ; load constant cb to accu 
push,r a ; push accu to the stack 
call sum ; call sub-routine sum 
sta vc ; vc = accu 
inc,r sp,2 ; deallocate parameter stack 
jmp _end ; jmp to label end 
_sum: 
push,r fp ; save old framepointer 
ldr fp,sp ; mark current frame 
dec,r sp,1 ; space for temp below the fp 
lda,c 0 ; accu = 0 
sta,ri fp,-1 ; temp = accu 
lda,ri fp,2 ; accu = 7 (via stack) 
add,ri fp,3 ; accu = 7 + 5 (via stack) 
sta,ri fp,-1 ; temp = accu 
lda,ri fp,-1 ; accu = temp 
ldr sp,fp ; remove locals from stack 
pop,r fp ; restore fp of caller 
ret ; back to caller 
_end: 
.end 


so soll die neue datei dann ausschauen: 

0x000: 
0x002: 
0x004: 
0x006: 
0x008: 
0x010: 
0x012: 
0x014: 
0x016: 
0x018: 
0x020: 
0x022: 
0x024: 
0x026: 
0x028: 
0x030: 
0x032: 
0x034: 
0x036: 
0x038: 
0x040: 
... 
0x080: 
lda,c 5 
push,r a 
lda,c 7 
push,r a 
call 0x016 
sta 0x080 
inc,r sp,2 
jmp 0x040 
push,r fp 
ldr fp,sp 
dec,r sp,1 
lda,c 0 
sta,ri fp,-1 
lda,ri fp,2 
add,ri fp,3 
sta,ri fp,-1 
lda,ri fp,-1 
ldr sp,fp 
pop,r fp 
ret 
.end 
... 
0

Mfg
Kebapmaster
 
Zuletzt bearbeitet von einem Moderator:
Grundsätzlich musst du den "Text" bis zu "_main:" durchsuchen. In diesem Abschnitt suchst du nach dem Schlüsswort ".equ". Wenn du dieses gefunden hast kannst du z.B. mit einem Iterator nach vorne gehen und den Wert speichern. Dann gehst du zurück und speicherst den Namen der Variable. In einem 2 Schritt vergleichst du dann die Liste von Variablen mit dem 2 Abschnitt des Textes und sobal eine Übereinstimmung gefunden wird, ersetz du die Variable

In C++ gibt es auch spezielle Bibliotheken, die dir das Leben mit solchen Sachen einfacher machen. (Boost::Spirit)

EDIT: Das gleiche natürlich dann mit dem Schlüsselwort ".dw" wobei du bei diesem die Anzahl Werte auslesen musst diese dann ausgibst und falls bei einem ein Wert angegeben wurde diesen dann einsezt

EDIT2: UNd so geht das mit allen aufgaben (Kommentare ignorieren sehr wichtig!!!,
 
Zuletzt bearbeitet von einem Moderator:
könntest du mir vll einen ansatz sagen bzw schreiben wie ich das in c programmieren soll. bin nämlich ein neukling in c und kenne mich überhaupt nicht aus!!!

mfg
kebapmaster
 
Also was ich machen kann sind ansätze in c++

PHP:
if(*actpos==';') {
*pointer1=*actpos;
while(*actpos!='\'' && *actpos!='n') {
*actpos++
}
*pointer2=*actpos +2;
}
vector.erase(pointer1,pointer2);

irgendwie sowas. Ich müsste die Konstrukte nach schauen und auch noch n paar andere Sachen aber so kann man zum beispiel alles rausfilter was ein Kommentar ist
 
Status
Für weitere Antworten geschlossen.
Zurück
Oben Unten