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 Maskinsprog main index: 0 method area: 40 bytes .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= 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; 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
Symbolsk maskinsprog Symbolske ordrer .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= 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; end_if: iload r // return r; ireturn Symbolske ordrer 1-1 afbildning mellem maskinordre og symbolsk maskinordre
? Hvor finder du bipush 88 ? Maskinsprog main index: 0 .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= 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; 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
Symbolsk maskinsprog bipush, iload 1-1 afbildning Symbolske ordrer .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= 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; end_if: iload r // return r; ireturn Symbolske ordrer bipush, iload 1-1 afbildning Direktiver/pseudoinstruktioner .define a=1 symbolsk konstant .method min metode start .args 3 antal argumenter .locals 1 antal lokale variable
Symbolsk maskinsprog bipush, iload 1-1 afbildning Symbolske ordrer .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= 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; end_if: iload r // return r; ireturn Symbolske ordrer bipush, iload 1-1 afbildning Direktiver/pseudoinstruktioner .define a=1 symbolsk konstant .method min metode start .args 3 antal argumenter .locals 1 antal lokale variable Labels else: end_if:
Symbolsk maskinsprog bipush, iload 1-1 afbildning Symbolske ordrer .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= 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; end_if: iload r // return r; ireturn Symbolske ordrer bipush, iload 1-1 afbildning Direktiver/pseudoinstruktioner .define a=1 symbolsk konstant .method min metode start .args 3 antal argumenter .locals 1 antal lokale variable Labels else: end_if: Symbolske operander a, b, r symbolske konstant min metodenavn else, end_if label
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 jmp endif # else else: movl %ebx, m # m = b endif: movl m, %ebx movl $1, %eax int $0x80 # return m
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 jmp endif # else else: movl %ebx, m # m = b endif: movl m, %ebx movl $1, %eax int $0x80 # return m Symbolske ordrer movl, cpml, jle, ... Direktiver .section .globl Labels _start: if: Symbolske operander a, b, m %eax, %ebx Samme struktur som program i IJVM symbolsk maskinsprog
Forskellige assembleres realisering af N = I + J Pentium Motorola 680x0 SPARC SCO figur 7.2
Symbolske maskinsprog Asm-niveauet realiseres af assembleren. Der kan sagtens være flere realiseringen ovenpå samme ISA-niveau ... .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; end_if: iload r // return r; ireturn // } .def min[3,1] # int min .eq a = 1 .eq b = 2 .eq r = 3 ld a # if ( a >= b ) ld b sub ld b # r = b; sto r jmp end_if else: # else ld a # r = a; ld r # return r; ret # } Forskellige direktiver, symbolsk ordrenavne, ...
.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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) isub iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; 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 01 isub iflt 10 istore 3 goto 7 iload 3 _constant pool_ 00000000 0000000e .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; 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 01 isub iflt 10 istore 3 goto 7 iload 3 _constant pool_ 00000000 0000000e .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 ){ .locals 1 // int r; .define r = 3 iload a // if ( a >= b ) iflt else iload b // r = b; istore r goto end_if else: // else iload a // r = a; end_if: iload r // return r; ireturn // } “Abstraktionsniveauet” i det symbolske maskin- sprog bestemmes af assembleren ...
Et symbolsk maskinsprog til IJVM (dComNet-nr.2)
IJVM simulatoren ordresæt Tilsvarende figur 4-11 i SCO, men med en mere detaljet beskrivelse af operander...
Programmering i symbolsk maskinsprog ... Effektive og maskinnære programmer, men besværligt ... ... skal bruges med omtanke ... ... programmering af ydre enheder, oversættere ...
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
dComNet-nr.1
Hvad gør dette
Målgruppen er MENNESKER
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;
Metode int gcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; .method gcd // int gcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // }
Metode – Ordning af parametre .method gcd // int gcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // } Caller's LV Caller's PC x y Link prt SP LV +2 +1 Gennemgå invokevirtual 1 på tavle, få det til at passe med lagerindholdet jvf. tegning på anden tavle. Vigtigt at have en konvention for rækkefølgen!!
Metode – Ordning af parametre .method gcd // int gcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // } Caller's LV Caller's PC x y Link prt SP LV +2 +1 Gennemgå invokevirtual 1 på tavle, få det til at passe med lagerindholdet jvf. tegning på anden tavle. IJVM konvention
Metode Caller's LV Caller's PC y x Link prt SP +2 +1 LV int gcd(int x, int y) { while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x; Caller's LV Caller's PC y x Link prt SP LV +2 +1 .method gcd // int gcd .args 3 // ( int x, int y) .locals 0 .define x = 1 .define y = 2 // { ... // }
Sidemandsopgave Over sæt Caller's LV Caller's PC y x Link prt SP +2 +1 int imul(int a, int b) { int p; p = 0; while ( a < 0 ){ a = a - 1; p = p + b; } return p; Caller's LV Caller's PC y x Link prt SP LV +2 +1 Over sæt ??? // { ... // }
Kontrolstruktur / while-løkke int gcd(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_icmpeq end_while // ( x != y ) // { ... goto while // } end_while:
Sidemandsopgave Over sæt int imul(int a, int b) { int p; p = 0; while ( a < 0 ){ a = a - 1; p = p + b; } return p; Over sæt ??? // { ... // }
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:
Beregning af udtryk, variabletildeling og returnvæ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; istore x // x = x - y; iload x ireturn // return x;
Sidemandsopgave Over sæt int imul(int a, int b) { int p; p = 0; while ( a < 0 ){ a = a - 1; p = p + b; } return p; Over sæt ??? // { ... // }
Metode og metodekald Caller's LV Caller's PC y x Link prt SP +2 +1 LV int main() { return gcd(2,3); } Caller's LV Caller's PC y x Link prt SP LV +2 +1 .method main // int main() .args 1 .locals 0 //{ // push object reference bipush 88 // push arguments bipush 2 bipush 3 invokevirtual gcd ireturn // return gcd(2,3) // }
Sidemandsopgave Over sæt Caller's LV Caller's PC b a Link prt SP +2 +1 int main() { return imul(2,3); } Caller's LV Caller's PC b a Link prt SP LV +2 +1 Over sæt ???
// Greatest common divisor. .method gcd // int gcd .args 3 // ( int x. int y) .define x = 1 .define y = 2 // { while: // while iload x iload y if_icmpeq end_while // ( x != y ) iload x // if iload y isub // stack = x-y, x-y < 0 => ( x < y ) iflt then goto else then: // { iload x isub istore y // y = y - x; goto end_if // } else: // else iload x // { istore x // x = x - y; // } end_if: goto while // } end_while: ireturn // return x; // } .method main bipush 88 bipush 2 bipush 3 invokevirtual gcd ireturn // return gcd(2,3)
gcd.j oversættes til ijvm-asm gcd.j gcd.bc fortolkes af ijvm 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
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: Der er fire fejl på stakken – hvilke og hvad er det rigtige? 9: her skal loades tre I stedet for to (anden parameter fra bunden) 15: Her fjernes øverste argument ikke 23: sub trækker fra omvendt (skal være 1) 32: her loades forkert (skal være 1)
Afprøvning af programmer Ekstern afprøvning Sammenlign et af programmet beregnet resultat med kendt og korrekt resultat ... Intern afprøvning Alle dele af programmet skal være gennemløbet og alle betingede hop skal være afviklet med betingelsen opfyldt og ikke opfyldt ...
ijvm gcd.bc IJVM Trace of gcd.bc stack = 0, 1, 18 bipush 88 [10 58] stack = 88, 0, 1, 18 bipush 2 [10 02] stack = 2, 88, 0, 1, 18 bipush 3 [10 03] stack = 3, 2, 88, 0, 1, 18 invokevirtual 0 [b6 00 00] stack = 17, 58, 3, 2, 23, 0, 1, 18 iload 1 [15 01] stack = 2, 17, 58, 3, 2, 23, 0, 1 iload 2 [15 02] stack = 3, 2, 17, 58, 3, 2, 23, 0 if_icmpeq 34 [9f 00 22] stack = 17, 58, 3, 2, 23, 0, 1, 18 isub [64] stack = -1, 17, 58, 3, 2, 23, 0, 1 iflt 6 [9b 00 06] stack = 17, 58, 3, 2, 23, 0, 1, 18 iload 2 [15 02] stack = 3, 17, 58, 3, 2, 23, 0, 1 iload 1 [15 01] stack = 2, 3, 17, 58, 3, 2, 23, 0 isub [64] stack = 1, 17, 58, 3, 2, 23, 0, 1 istore 2 [36 02] stack = 17, 58, 1, 2, 23, 0, 1, 18 goto 10 [a7 00 0a] stack = 17, 58, 1, 2, 23, 0, 1, 18 goto -35 [a7 ff dd] stack = 17, 58, 1, 2, 23, 0, 1, 18 iload 1 [15 01] stack = 2, 17, 58, 1, 2, 23, 0, 1 iload 2 [15 02] stack = 1, 2, 17, 58, 1, 2, 23, 0 if_icmpeq 34 [9f 00 22] stack = 17, 58, 1, 2, 23, 0, 1, 18 isub [64] stack = 1, 17, 58, 1, 2, 23, 0, 1 iflt 6 [9b 00 06] stack = 17, 58, 1, 2, 23, 0, 1, 18 goto 13 [a7 00 0d] stack = 17, 58, 1, 2, 23, 0, 1, 18 istore 1 [36 01] stack = 17, 58, 1, 1, 23, 0, 1, 18 goto -35 [a7 ff dd] stack = 17, 58, 1, 1, 23, 0, 1, 18 iload 1 [15 01] stack = 1, 17, 58, 1, 1, 23, 0, 1 iload 2 [15 02] stack = 1, 1, 17, 58, 1, 1, 23, 0 if_icmpeq 34 [9f 00 22] stack = 17, 58, 1, 1, 23, 0, 1, 18 ireturn [ac] stack = 1, 0, 1, 18 ireturn [ac] stack = 1 ijvm gcd.bc
Evt. optimeringer foretages altid på et korrekt program ... Arbejdsgang ... Tag udgangspunkt i et C/java program Oversæt systematisk til symbolsk maskinsprog Kommenter med C/java programmet Afprøv programmet eksternt og internt Evt. optimeringer foretages altid på et korrekt program ...