symbolsk maskinsprog
Post on 15-Jan-2016
25 Views
Preview:
DESCRIPTION
TRANSCRIPT
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 maskinsprogtil absolut maskinsprog (næste gang)
.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
// }
main index: 0method area: 40 bytes00 03 00 00 10 58 15 01 15 02 b6 00 01 ac 00 0300 01 15 01 15 02 64 9b 00 0a 15 02 36 03 a7 0007 15 01 36 03 15 03 acconstant pool: 2 words000000000000000e
Maskinsprog
Symbolsk maskinsprog
.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
// }
Symbolsk maskinsprogSymbolske ordrer
1-1 afbildning mellem maskinordre og symbolsk maskinordre
.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
// }
main index: 0method area: 40 bytes00 03 00 00 10 58 15 01 15 02 b6 00 01 ac 00 0300 01 15 01 15 02 64 9b 00 0a 15 02 36 03 a7 0007 15 01 36 03 15 03 acconstant pool: 2 words000000000000000e
Maskinsprog
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 goto end_if
else: // else iload a // r = a; istore r
end_if: iload r // return r; ireturn
// }
Symbolsk maskinsprogSymbolske 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
.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
// }
Symbolsk maskinsprogSymbolske 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
Labelselse:end_if:
.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
// }
Symbolsk maskinsprogSymbolske 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
Labelselse:end_if:
Symbolske operandera, b, r symbolske konstantmin metodenavnelse, end_if label
Pentium symbolsk maskinsprog
.section .data # start of data sectiona: .long 42 # variable ab: .long 53 # variable bm: .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
# elseelse: movl %ebx, m # m = b
endif: movl m, %ebxmovl $1, %eaxint $0x80 # return m
Pentium symbolsk maskinsprog
.section .data # start of data sectiona: .long 42 # variable ab: .long 53 # variable bm: .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
# elseelse: movl %ebx, m # m = b
endif: movl m, %ebx movl $1, %eax int $0x80 # return m
Symbolske ordrermovl, cpml, jle, ...
Direktiver.section.globl
Labels_start:if:
Symbolske operandera, b, m%eax, %ebx
Samme struktur som program i IJVM symbolsk maskinsprog
SCO figur 7.2
Forsk
ellige assemb
leres realiserin
g af N =
I + J
Symbolske maskinsprogAsm-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_ifelse: // else iload a // r = a; istore rend_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 iflt else ld b # r = b; sto r jmp end_ifelse: # else ld a # r = a; sto rend_if: 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 ){.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_ifelse: // else iload a // r = a; istore rend_if: iload r // return r; ireturn // }
_main index_ 0
_method area_
00 0300 00
bipush 88iload 1iload 2invokevirtual 1ireturn
00 0300 01
iload 1iload 2isubiflt 10iload 2istore 3goto 7iload 1istore 3iload 3ireturn
_constant pool_
000000000000000e
.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_ifelse: // else iload a // r = a; istore rend_if: iload r // return r; ireturn // }
_main index_ 0
_method area_
00 0300 00
bipush 88iload 1iload 2invokevirtual 1ireturn
00 0300 01
iload 1iload 2isubiflt 10iload 2istore 3goto 7iload 1istore 3iload 3ireturn
_constant pool_
000000000000000e
.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_ifelse: // else iload a // r = a; istore rend_if: iload r // return r; ireturn // }
“Abstraktionsniveauet” i det symbolske maskin- sprog bestemmes af assembleren ...
Et symbolsk maskinsprog til IJVM (dComNet-nr.2)
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 maskinsprogint 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ørelsevariabel erklæringer med simple typerkontrol strukturer; if-then-else, while
beregning af betingelser og udtryk
dComNet-nr.1
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;}
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
Metode – Ordning af parametre
Vigtigt at have en konvention for rækkefølgen!!
Caller's LVCaller's PC
xy
Link prt
SP
LV
Caller's LVCaller's PC
yx
Link prt
SP
LV
+2+1
+2+1
.method gcd // int gcd
.args 3 // ( int x, int y)
.locals 0
.define x = 1
.define y = 2 // {... // }
Metode – Ordning af parametre
IJVM konvention
.method gcd // int gcd
.args 3 // ( int x, int y)
.locals 0
.define x = 1
.define y = 2 // {... // }
Caller's LVCaller's PC
xy
Link prt
SP
LV
Caller's LVCaller's PC
yx
Link prt
SP
LV
+2+1
+2+1
int gcd(int x, int y){ while ( x != y ){ if ( x < y ) y = y - x; else x = x - y; } return x;}
Metode
Caller's LVCaller's PC
yx
Link prt
SP
LV
+2+1
.method gcd // int gcd
.args 3 // ( int x, int y)
.locals 0
.define x = 1
.define y = 2 // {
... // }
int imul(int a, int b){ int p;
p = 0; while ( a < 0 ){ a = a - 1; p = p + b; } return p;}
Sidemandsopgave
Caller's LVCaller's PC
yx
Link prt
SP
LV
+2+1
??? // {
... // }
Over sæt
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:
Kontrolstruktur / while-løkke
int imul(int a, int b){ int p;
p = 0; while ( a < 0 ){ a = a - 1; p = p + b; } return p;}
Sidemandsopgave
??? // {
... // }
Over sæt
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 elsethen: // { ...
goto end_if // }else: // else ... // { // }end_if:
...
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 y iload x isub istore y // y = y - x;
iload x iload y isub istore x // x = x - y;
iload xireturn // return x;
Beregning af udtryk, variabletildeling og returnværdi
int imul(int a, int b){ int p;
p = 0; while ( a < 0 ){ a = a - 1; p = p + b; } return p;}
Sidemandsopgave
??? // {
... // }
Over sæt
int main(){ return gcd(2,3);}
.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) // }
Metode og metodekald
Caller's LVCaller's PC
yx
Link prt
SP
LV
+2+1
int main(){ return imul(2,3);}
???
Sidemandsopgave
Caller's LVCaller's PC
ba
Link prt
SP
LV
+2+1Over
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 elsethen: // { iload y iload x isub istore y // y = y - x; goto end_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 bipush 88 bipush 2 bipush 3 invokevirtual gcd ireturn // return gcd(2,3)
ijvm-asm gcd.j gcd.bc
ijvm gcd.bc
gcd.j
oversættes til
fortolkes af
IJVM maskine
main index: 1method area: 59 bytes00 03 00 00 15 01 15 02 9f 00 22 15 01 15 02 649b 00 06 a7 00 0d 15 02 15 01 64 36 02 a7 00 0a15 01 15 02 64 36 01 a7 ff dd 15 01 ac 00 01 0000 10 58 10 02 10 03 b6 00 00 acconstant pool: 2 words000000000000002d
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, 010:isub [64] stack = -1, 17, 58, 3, 2, 23, 0, 111:iflt 6 [9b 00 06] stack = 17, 58, 3, 2, 23, 0, 1, 1812:iload 2 [15 02] stack = 3, 17, 58, 3, 2, 23, 0, 113:iload 1 [15 01] stack = 2, 3, 17, 58, 3, 2, 23, 014:isub [64] stack = 1, 17, 58, 3, 2, 23, 0, 115:istore 2 [36 02] stack = 1, 17, 58, 1, 2, 23, 0, 1, 1816:goto 10 [a7 00 0a] stack = 17, 58, 1, 2, 23, 0, 1, 1817:goto -35 [a7 ff dd] stack = 17, 58, 1, 2, 23, 0, 1, 1818:iload 1 [15 01] stack = 2, 17, 58, 1, 2, 23, 0, 119:iload 2 [15 02] stack = 1, 2, 17, 58, 1, 2, 23, 020:if_icmpeq 34 [9f 00 22] stack = 17, 58, 1, 2, 23, 0, 1, 1821:iload 1 [15 01] stack = 2, 17, 58, 1, 2, 23, 0, 122:iload 2 [15 02] stack = 1, 2, 17, 58, 1, 2, 23, 023:isub [64] stack = -1, 17, 58, 1, 2, 23, 0, 124:iflt 6 [9b 00 06] stack = 17, 58, 1, 2, 23, 0, 1, 1825:goto 13 [a7 00 0d] stack = 17, 58, 1, 2, 23, 0, 1, 1826:iload 1 [15 01] stack = 2, 17, 58, 1, 2, 23, 0, 127:iload 2 [15 02] stack = 1, 2, 17, 58, 1, 2, 23, 028:isub [64] stack = 1, 17, 58, 1, 2, 23, 0, 129:istore 1 [36 01] stack = 17, 58, 1, 1, 23, 0, 1, 1830:goto -35 [a7 ff dd] stack = 17, 58, 1, 1, 23, 0, 1, 1831:iload 1 [15 01] stack = 1, 17, 58, 1, 1, 23, 0, 132:iload 2 [15 02] stack = 786, 1, 17, 58, 1, 1, 23, 033:if_icmpeq 34 [9f 00 22] stack = 17, 58, 1, 1, 23, 0, 1, 1834:iload 1 [15 01] stack = 1, 17, 58, 1, 1, 23, 0, 135:ireturn [ac] stack = 1, 0, 1, 1836:ireturn [ac] stack = 1
Diskutér m
ed din sidemandd: D
er er fire fejl på stakken – hvilke og hvad er det rigtige?
ijvm gcd.bc
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 ...
Arbejdsgang ...
Evt. optimeringer foretages altid på et korrekt program ...
Tag udgangspunkt i et C/java program
Oversæt systematisk til symbolsk maskinsprog
Kommenter med C/java programmet
Afprøv programmet eksternt og internt
top related