CPU Simulator

  • geschlossen
  • C

  • kebapmaster
  • 1368 Aufrufe 3 Antworten

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • CPU Simulator

    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.

    Quellcode

    1. ; a simple program that adds two constants using a sub-routine and stores
    2. ; the result in a variable vc
    3. ;
    4. ca .equ 5 ; define a constant ca with value 5
    5. cb .equ 7 ; define a constant cb with value 7
    6. .org 128
    7. vc .dw 0 ; define a variable vc at address 128 (= 0x80) with value 0
    8. .org 0
    9. _main:
    10. lda,c ca ; load constant ca to accu
    11. push,r a ; push accu to the stack
    12. lda,c cb ; load constant cb to accu
    13. push,r a ; push accu to the stack
    14. call sum ; call sub-routine sum
    15. sta vc ; vc = accu
    16. inc,r sp,2 ; deallocate parameter stack
    17. jmp _end ; jmp to label end
    18. _sum:
    19. push,r fp ; save old framepointer
    20. ldr fp,sp ; mark current frame
    21. dec,r sp,1 ; space for temp below the fp
    22. lda,c 0 ; accu = 0
    23. sta,ri fp,-1 ; temp = accu
    24. lda,ri fp,2 ; accu = 7 (via stack)
    25. add,ri fp,3 ; accu = 7 + 5 (via stack)
    26. sta,ri fp,-1 ; temp = accu
    27. lda,ri fp,-1 ; accu = temp
    28. ldr sp,fp ; remove locals from stack
    29. pop,r fp ; restore fp of caller
    30. ret ; back to caller
    31. _end:
    32. .end
    33. so soll die neue datei dann ausschauen:
    34. 0x000:
    35. 0x002:
    36. 0x004:
    37. 0x006:
    38. 0x008:
    39. 0x010:
    40. 0x012:
    41. 0x014:
    42. 0x016:
    43. 0x018:
    44. 0x020:
    45. 0x022:
    46. 0x024:
    47. 0x026:
    48. 0x028:
    49. 0x030:
    50. 0x032:
    51. 0x034:
    52. 0x036:
    53. 0x038:
    54. 0x040:
    55. ...
    56. 0x080:
    57. lda,c 5
    58. push,r a
    59. lda,c 7
    60. push,r a
    61. call 0x016
    62. sta 0x080
    63. inc,r sp,2
    64. jmp 0x040
    65. push,r fp
    66. ldr fp,sp
    67. dec,r sp,1
    68. lda,c 0
    69. sta,ri fp,-1
    70. lda,ri fp,2
    71. add,ri fp,3
    72. sta,ri fp,-1
    73. lda,ri fp,-1
    74. ldr sp,fp
    75. pop,r fp
    76. ret
    77. .end
    78. ...
    79. 0
    Alles anzeigen


    Mfg
    Kebapmaster

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Broken Sword () aus folgendem Grund: code-Tag hinzugefügt

  • 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!!!,

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von Xbelt ()

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

    PHP-Quellcode

    1. if(*actpos==';') {
    2. *pointer1=*actpos;
    3. while(*actpos!='\'' && *actpos!='n') {
    4. *actpos++
    5. }
    6. *pointer2=*actpos +2;
    7. }
    8. 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