1 / 40

Symbolsk maskinsprog

Symbolsk maskinsprog. Niveauer af abstrakte maskiner. Spørgsmål ... Hvordan bruges asm-niveauet? metode til programmering (i dag) Hvordan stilles asm-niveauet tilrådighed? oversættelse af symbolsk maskinsprog til absolut maskinsprog (næste gang). Symbolsk maskinsprog.

lihua
Download Presentation

Symbolsk maskinsprog

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Symbolsk maskinsprog

  2. Niveauerafabstraktemaskiner Spørgsmål ... Hvordan bruges asm-niveauet? metode til programmering (i dag) Hvordan stilles asm-niveauet tilrådighed? oversættelse af symbolsk maskinsprog til absolut maskinsprog (næste gang)

  3. Symbolskmaskinsprog .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 // Push object reference. iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub // stack = a - b, ... ; a - b < 0 => a < b iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // } Maskinsprog main index: 0 method area: 40 bytes 00 03 00 00 10 58 15 01 15 02 b6 00 01 ac 00 03 00 01 15 01 15 02 64 9b 00 0a 15 02 36 03 a7 00 07 15 01 36 03 15 03 ac constant pool: 2 words 00000000 0000000e

  4. Symbolskmaskinsprog .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 // Push object reference. iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub // stack = a - b, ... ; a - b < 0 => a < b iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // } Symbolske ordrer 1-1 afbildning mellem maskinordre og symbolsk maskinordre

  5. Hvor finder du bipush 88 ? .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 // Push object reference. iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub // stack = a - b, ... ; a - b < 0 => a < b iflt else iload b // r = b; istore r gotoend_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // } Maskinsprog ? main index: 0 method area: 40 bytes 00 03 00 00 10 58 15 01 15 02 b6 00 01 ac 00 03 00 01 15 01 15 02 64 9b 00 0a 15 02 36 03 a7 00 07 15 01 36 03 15 03 ac constant pool: 2 words 00000000 0000000e

  6. Symbolskmaskinsprog .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 // Push object reference. iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub // stack = a - b, ... ; a - b < 0 => a < b iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // } Symbolskeordrer bipush, iload1-1 afbildning Direktiver/pseudoinstruktioner .define a=1 symbolskkonstant .method min metode start .args 3 antalargumenter .locals 1 antallokale variable

  7. Symbolskmaskinsprog .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 // Push object reference. iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub // stack = a - b, ... ; a - b < 0 => a < b iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // } Symbolskeordrer bipush, iload1-1 afbildning Direktiver/pseudoinstruktioner • .define a=1 symbolskkonstant • .method min metode start • .args 3 antalargumenter • .locals 1 antallokale variable Labels else: end_if:

  8. Symbolskmaskinsprog .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 // Push object reference. iloada iloadb invokevirtualmin ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iloada // if ( a >= b ) iloadb isub // stack = a - b, ... ; a - b < 0 => a < b ifltelse iloadb // r = b; istorer gotoend_if else: // else iloada // r = a; istorer end_if: iloadr // return r; ireturn // } Symbolskeordrer bipush, iload1-1 afbildning Direktiver/pseudoinstruktioner • .define a=1 symbolskkonstant • .method min metode start • .args 3 antalargumenter • .locals 1 antallokale variable Labels else: end_if: Symbolskeoperander a, b, r symbolskekonstant min metodenavn else, end_iflabel

  9. Pentium symbolsk maskinsprog .section .data # start of data section a: .long 42 # variable a b: .long 53 # variable b m: .long 0 # variable m .section .text # start of text section .globl _start # _start is a global symbol # specifying program start _start: # int main (void) movl a, %eax # movl b, %ebx # cpml %eax, %ebx # compute b-a <= 0 and # set eflags jle if # if (a >= b) jmp else if: movl %eax, m # m = a jmpendif # else else: movl %ebx, m # m = b endif: movl m, %ebx movl $1, %eax int $0x80 # return m

  10. Pentium symbolsk maskinsprog Symbolske ordrer movl, cpml, jle, ... Direktiver .section .globl Labels _start: if: Symbolske operander a, b, m %eax, %ebx .section .data# start of data section a: .long 42# variable a b: .long 53# variable b m: .long 0# variable m .section .text# start of text section .globl_start# _start is a global symbol # specifying program start _start:# int main (void) movla, %eax# movlb, %ebx# cpml%eax, %ebx# compute b-a <= 0 and # set eflags jleif# if (a >= b) jmpelse if:movl%eax, m# m = a jmpendif # else else:movl%ebx, m# m = b endif:movlm, %ebx movl$1, %eax int$0x80# return m Samme struktur som program i IJVM symbolsk maskinsprog

  11. Forskelligeassembleres realiseringaf N = I + J SCO figur 7.2

  12. Symbolskemaskinsprog Asm-niveauet realiseres af assembleren. Der kan sagtens være flere realiseringen ovenpå samme ISA-niveau ... .def min[3,1] # int min .eq a = 1 .eq b = 2 .eq r = 3 lda # if ( a >= b ) ldb sub ifltelse ldb # r = b; stor jmpend_if else: # else lda # r = a; stor end_if: ldr # return r; ret # } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iloada // if ( a >= b ) iloadb isub ifltelse iloadb // r = b; istorer gotoend_if else: // else iloada // r = a; istorer end_if: iloadr // return r; ireturn // } Forskellige direktiver, symbolsk ordrenavne, ...

  13. .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // }

  14. .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // } _main index_ 0 _method area_ 00 03 00 00 bipush 88 iload 1 iload 2 invokevirtual 1 ireturn 00 03 00 01 iload 1 iload 2 isub iflt 10 iload 2 istore 3 goto 7 iload 1 istore 3 iload 3 ireturn _constant pool_ 00000000 0000000e

  15. .method main // int main .args 3 // ( int a, int b ) .define a = 1 .define b = 2 // { bipush 88 iload a iload b invokevirtual min ireturn // return min ( a, b ); // } .method min // int min .args 3 // ( int a, int b ){ .define a = 1 .define b = 2 .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iload b isub iflt else iload b // r = b; istore r gotoend_if else: // else iload a // r = a; istore r end_if: iload r // return r; ireturn // } _main index_ 0 _method area_ 00 03 00 00 bipush 88 iload 1 iload 2 invokevirtual 1 ireturn 00 03 00 01 iload 1 iload 2 isub iflt 10 iload 2 istore 3 goto 7 iload 1 istore 3 iload 3 ireturn _constant pool_ 00000000 0000000e “Abstraktionsniveauet” idetsymbolskemaskin- sprogbestemmesafassembleren ...

  16. Et symbolsk maskinsprog til IJVM (dComNet-nr.2)

  17. Programmering i symbolsk maskinsprog ... Effektive og maskinnære programmer, men besværligt ... ... skal bruges med omtanke ... ... programmering af ydre enheder, oversættere ...

  18. Programmering i symbolsk maskinsprog int main() { return gcd(2,3); } int gcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; } ... systematisk oversættelse af højniveau-konstruktioner ... metoder med parameteroverførelse variabel erklæringer med simple typer kontrol strukturer; if-then-else, while beregning af betingelser og udtryk

  19. dComNet-nr.1

  20. Eksempel int main() { return gcd(2,3); } int gcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; }

  21. Metode intgcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; } .method gcd // intgcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // }

  22. Metode – Ordningafparametre .method gcd // int gcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // } SP SP Caller's LV Caller's PC x y Link prt Caller's LV Caller's PC y x Link prt +2 +1 +2 +1 LV LV Vigtigt at have en konvention for rækkefølgen!!

  23. Metode – Ordningafparametre .method gcd // int gcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // } SP SP Caller's LV Caller's PC x y Link prt Caller's LV Caller's PC y x Link prt +2 +1 +2 +1 LV LV IJVM konvention

  24. Metode int gcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; } SP Caller's LV Caller's PC y x Link prt +2 +1 LV .method gcd // intgcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // }

  25. Sidemandsopgave intimul(int a, int b) { int p; p = 0; while ( a < 0 ){ a = a - 1; p = p+b; } return p; } SP Caller's LV Caller's PC y x Link prt +2 +1 LV Over sæt ??? // { ... // }

  26. Kontrolstruktur / while-løkke intgcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; } while: // while iload x iload y if_icmpeqend_while // ( x != y ) // { ... goto while // } end_while:

  27. Sidemandsopgave intimul(int a, int b) { int p; p = 0; while ( a < 0 ){ a = a - 1; p = p+b; } return p; } Over sæt ??? // { ... // }

  28. Kontrolstruktur / if-then-else int gcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; } iload x // if iload y isub // stack = x-y, x-y < 0 => ( x < y ) iflt then goto else then: // { ... goto end_if // } else: // else ... // { // } end_if: ...

  29. Beregningafudtryk, variabletildelingogreturnværdi int gcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; } iload y iload x isub istore y // y = y - x; iload x iload y isub istore x // x = x - y; iload x ireturn // return x;

  30. Sidemandsopgave intimul(int a, int b) { int p; p = 0; while ( a < 0 ){ a = a - 1; p = p + b; } return p; } Over sæt ??? // { ... // }

  31. Metodeogmetodekald int main() { return gcd(2,3); } SP Caller's LV Caller's PC y x Link prt +2 +1 LV .method main // int main() .args 1 .locals 0 //{ // push object reference bipush 88 // push arguments bipush 2 bipush 3 invokevirtualgcd ireturn // return gcd(2,3) // }

  32. Sidemandsopgave int main() { return imul(2,3); } SP Caller's LV Caller's PC b a Link prt +2 +1 Over sæt LV ???

  33. // Greatest common divisor. .method gcd // intgcd .args 3 // ( int x. int y) .define x = 1 .define y = 2 // { while: // while iload x iload y if_icmpeqend_while // ( x != y ) // { iload x // if iload y isub // stack = x-y, x-y < 0 => ( x < y ) iflt then goto else then: // { iload y iload x isub istore y // y = y - x; gotoend_if// } else: // else iload x // { iload y isub istore x // x = x - y; // } end_if: goto while // } end_while: iload x ireturn // return x; // } .method main bipush88 bipush 2 bipush 3 invokevirtualgcd ireturn// return gcd(2,3)

  34. gcd.j oversættes til ijvm-asmgcd.j gcd.bc main index: 1 method area: 59 bytes 00 03 00 00 15 01 15 02 9f 00 22 15 01 15 02 64 9b 00 06 a7 00 0d 15 02 15 01 64 36 02 a7 00 0a 15 01 15 02 64 36 01 a7 ff dd 15 01 ac 00 01 00 00 10 58 10 02 10 03 b6 00 00 ac constant pool: 2 words 00000000 0000002d fortolkes af ijvm gcd.bc IJVM maskine

  35. ijvm gcd.bc IJVM Trace of gcd.bc stack = 0, 1, 18 1:bipush 88 [10 58] stack = 88, 0, 1, 18 2:bipush 2 [10 02] stack = 2, 88, 0, 1, 18 3:bipush 3 [10 03] stack = 3, 2, 88, 0, 1, 18 4:invokevirtual 0 [b6 00 00] stack = 17, 58, 3, 2, 23, 0, 1, 18 5:iload 1 [15 01] stack = 2, 17, 58, 3, 2, 23, 0, 1 6:iload 2 [15 02] stack = 2, 2, 17, 58, 3, 2, 23, 0 7:if_icmpeq 34 [9f 00 22] stack = 17, 58, 3, 2, 23, 0, 1, 18 8:iload 1 [15 01] stack = 2, 17, 58, 3, 2, 23, 0, 1 9:iload 2 [15 02] stack = 3, 2, 17, 58, 3, 2, 23, 0 10:isub [64] stack = -1, 17, 58, 3, 2, 23, 0, 1 11:iflt 6 [9b 00 06] stack = 17, 58, 3, 2, 23, 0, 1, 18 12:iload 2 [15 02] stack = 3, 17, 58, 3, 2, 23, 0, 1 13:iload 1 [15 01] stack = 2, 3, 17, 58, 3, 2, 23, 0 14:isub [64] stack = 1, 17, 58, 3, 2, 23, 0, 1 15:istore 2 [36 02] stack = 1, 17, 58, 1, 2, 23, 0, 1, 18 16:goto 10 [a7 00 0a] stack = 17, 58, 1, 2, 23, 0, 1, 18 17:goto -35 [a7 ff dd] stack = 17, 58, 1, 2, 23, 0, 1, 18 18:iload 1 [15 01] stack = 2, 17, 58, 1, 2, 23, 0, 1 19:iload 2 [15 02] stack = 1, 2, 17, 58, 1, 2, 23, 0 20:if_icmpeq 34 [9f 00 22] stack = 17, 58, 1, 2, 23, 0, 1, 18 21:iload 1 [15 01] stack = 2, 17, 58, 1, 2, 23, 0, 1 22:iload 2 [15 02] stack = 1, 2, 17, 58, 1, 2, 23, 0 23:isub [64] stack = -1, 17, 58, 1, 2, 23, 0, 1 24:iflt 6 [9b 00 06] stack = 17, 58, 1, 2, 23, 0, 1, 18 25:goto 13 [a7 00 0d] stack = 17, 58, 1, 2, 23, 0, 1, 18 26:iload 1 [15 01] stack = 2, 17, 58, 1, 2, 23, 0, 1 27:iload 2 [15 02] stack = 1, 2, 17, 58, 1, 2, 23, 0 28:isub [64] stack = 1, 17, 58, 1, 2, 23, 0, 1 29:istore 1 [36 01] stack = 17, 58, 1, 1, 23, 0, 1, 18 30:goto -35 [a7 ff dd] stack = 17, 58, 1, 1, 23, 0, 1, 18 31:iload 1 [15 01] stack = 1, 17, 58, 1, 1, 23, 0, 1 32:iload 2 [15 02] stack = 786, 1, 17, 58, 1, 1, 23, 0 33:if_icmpeq 34 [9f 00 22] stack = 17, 58, 1, 1, 23, 0, 1, 18 34:iload 1 [15 01] stack = 1, 17, 58, 1, 1, 23, 0, 1 35:ireturn [ac] stack = 1, 0, 1, 18 36:ireturn [ac] stack = 1 Diskutér med din sidemandd: Derer fire fejlpåstakken – hvilkeoghvaderdetrigtige?

  36. Afprøvning af programmer Eksternafprøvning Sammenlignet afprogrammetberegnetresultat med kendtogkorrektresultat ... Intern afprøvning Alledele afprogrammetskalværegennemløbetogallebetingede hop skalværeafviklet med betingelsenopfyldtogikkeopfyldt ...

  37. Arbejdsgang ... Evt. optimeringer foretages altid på et korrekt program ... Tag udgangspunkti et C/java program Oversæt systematisktilsymbolskmaskinsprog Kommenter med C/java programmet Afprøvprogrammeteksterntoginternt

More Related