Re: [問題] object code的定義
==> newpuli.bbs@ptt.cc (= =) 提到:
> 對於object code的定義,
> 我似乎搞混了,
> 所以我想要請板上的各位們請教一下...
> 我目前對objcet code的認知是:
> 程式的source code會先被編譯成object code,
> object code裡面包含的內容就是機器指令,
> 然後這個程式如果要被執行,
> 他的object code會先被載入到記憶體裡,
> 然後就直接跑這個object code了.
> 不知我的認知是否有誤呢?
> 如果有誤的話,可否麻煩請指正一下,
> 看看我的說法要如何修改才是正確的呢?
> 如果我的想法是正確的話,
> 那我是不是可以說 "object code就等於是機器碼"呢?
> 謝謝~
參考一下早期 X86 16位元的 Compiler 出的 obj 檔案結構
(173) ID {0xA0},offset [0xF39] ,Size = 1028(0x404) -- Segment Data
- Segment 1 ,org 0x0
(174) ID {0x9C},offset [0x1340] ,Size = 785(0x311) -- Relocation List
- Ex: external segment & offset data - (group 3) [0x3FA] is load by OS
- Ex: external offset data - (seg 11) [0x3F7] is load by OS
- Ex: external offset data - (seg 12) [0x3F3] is load by OS
- Ex: external offset data - (seg 12) [0x3EC] is load by OS
- Ex: external offset data - (group 2) [0x3E6] is load by OS
int matherr(struct exception *a)
{
oper_rlt=-2;
return 1;
}
0C67:0F39 0100 ADD [BX+SI],AX
0C67:0F3B 00558B ADD [DI-75],DL
0C67:0F3E EC IN AL,DX
0C67:0F3F 1E PUSH DS
0C67:0F40 B80000 MOV AX,0000
0C67:0F43 8ED8 MOV DS,AX
0C67:0F45 C706D6D3FEFF MOV WORD PTR [D3D6],FFFE
0C67:0F4B B80100 MOV AX,0001
0C67:0F4E EB00 JMP 0F50
0C67:0F50 1F POP DS
0C67:0F51 5D POP BP
0C67:0F52 CB RETF
int oper_result(void)
{
return(oper_rlt);
}
0C67:0F53 55 PUSH BP
0C67:0F54 8BEC MOV BP,SP
0C67:0F56 1E PUSH DS
0C67:0F57 B80000 MOV AX,0000
0C67:0F5A 8ED8 MOV DS,AX
0C67:0F5C A1D6D3 MOV AX,[D3D6]
0C67:0F5F EB00 JMP 0F61
0C67:0F61 1F POP DS
0C67:0F62 5D POP BP
0C67:0F63 CB RETF
double A_to_N(char *string)
{
int i,k=35;
double num=0;
for(i=0;i<k && string[i]!=0;i++);
if(i==k || i==0) oper_rlt=-2;
else
{
oper_rlt=0;
num=atof(string);
if(num>1.78e308) oper_rlt=-2;
}
return(num);
}
0C67:0F64 55 PUSH BP
0C67:0F65 8BEC MOV BP,SP
0C67:0F67 83EC0A SUB SP,+0A
0C67:0F6A 56 PUSH SI
0C67:0F6B 57 PUSH DI
0C67:0F6C 1E PUSH DS
0C67:0F6D B80000 MOV AX,0000
0C67:0F70 8ED8 MOV DS,AX
0C67:0F72 BF2300 MOV DI,0023
0C67:0F75 9B WAIT
0C67:0F76 D9EE FLDZ
0C67:0F78 9B WAIT
0C67:0F79 DD5EF8 FSTP QWORD PTR [BP-08]
0C67:0F7C 33F6 XOR SI,SI
0C67:0F7E 90 NOP
0C67:0F7F 9B WAIT
0C67:0F80 EB01 JMP 0F83
0C67:0F82 46 INC SI
0C67:0F83 3BF7 CMP SI,DI
0C67:0F85 7D09 JGE 0F90
0C67:0F87 C45E06 LES BX,[BP+06]
0C67:0F8A 26 ES:
0C67:0F8B 803800 CMP BYTE PTR [BX+SI],00
0C67:0F8E 75F2 JNZ 0F82
0C67:0F90 3BF7 CMP SI,DI
0C67:0F92 7404 JZ 0F98
0C67:0F94 0BF6 OR SI,SI
0C67:0F96 7508 JNZ 0FA0
0C67:0F98 C706D6D3FEFF MOV WORD PTR [D3D6],FFFE
0C67:0F9E EB33 JMP 0FD3
0C67:0FA0 C706D6D30000 MOV WORD PTR [D3D6],0000
0C67:0FA6 FF7608 PUSH [BP+08]
0C67:0FA9 FF7606 PUSH [BP+06]
0C67:0FAC 9A00000000 CALL 0000:0000
0C67:0FB1 83C404 ADD SP,+04
0C67:0FB4 9B WAIT
0C67:0FB5 DD5EF8 FSTP QWORD PTR [BP-08]
0C67:0FB8 9B WAIT
0C67:0FB9 DD46F8 FLD QWORD PTR [BP-08]
0C67:0FBC 9B WAIT
0C67:0FBD DC1EE4D3 FCOMP QWORD PTR [D3E4]
0C67:0FC1 9B WAIT
0C67:0FC2 DD7EF6 FSTSW [BP-0A]
0C67:0FC5 90 NOP
0C67:0FC6 9B WAIT
0C67:0FC7 8B46F6 MOV AX,[BP-0A]
0C67:0FCA 9E SAHF
0C67:0FCB 7606 JBE 0FD3
0C67:0FCD C706D6D3FEFF MOV WORD PTR [D3D6],FFFE
0C67:0FD3 9B WAIT
0C67:0FD4 DD46F8 FLD QWORD PTR [BP-08]
0C67:0FD7 EB00 JMP 0FD9
0C67:0FD9 1F POP DS
0C67:0FDA 5F POP DI
0C67:0FDB 5E POP SI
0C67:0FDC C9 DB C9
0C67:0FDD CB RETF
這是以下的程式分析結果
10 input "Input .obj file name: ",a$
20 gosub 800
30 yes_obj=0
40 open "i",#1,a$
50 page_num=3
60 data1=asc(input$(1,#1))
70 file_location=file_location+1
80 if buff_index<100 then buff[buff_index]=data1
90 if buff_index<100 then buff_index=buff_index+1
100 block_sum=block_sum+data1
110 if file_location=1 and data1=0x80 then gosub 900
120 if yes_obj=1 and file_location=block_next then gosub 1000
130 if yes_obj=1 and file_location=block_next-1 then gosub 2000
140 if file_location=1 and yes_obj=0 then print "Bad .obj file!" : goto 160
150 if not eof(1) then goto 60
160 close #1
500 end
800 rem got file name
810 dot_flag=0
820 for i=1 to len(a$)
830 if mid$(a$,i,1)="." then dot_flag=1
840 next
850 if dot_flag=0 then a$=a$+".obj"
860 seg_info_num=0
870 seg_info[0]=0
880 return
900 rem check if obj file
910 print "Got Obj file:",a$
920 yes_obj=1
930 block_next=1
940 return
1000 rem get block_length
1010 block_id=data1
1020 data1=asc(input$(1,#1))
1030 data2=asc(input$(1,#1))
1040 file_location=file_location+2
1050 block_size=data2*256+data1
1060 block_next=block_next+3+block_size
1070 block_count=block_count+1
1080 block_sum=block_id+data1+data2
1090 print "(";block_count;") ID {0x";hex$(block_id);"},offset [0x";hex$(file_location);"]";" ,Size =",block_size;"(0x";hex$(block_size);")";
1100 buff_index=1
1120 return
2000 rem got block_type
block_sum=block_sum and 0xff
if block_id=0x80 then print " -- Module Name"
if block_id=0x8a then print " -- End of Module"
if block_id=0x8c then print " -- External Symbols"
if block_id=0x90 then print " -- Public Symbols"
if block_id=0x94 then print " -- Line Number Info"
if block_id=0x96 then print " -- Segment/Group Symbols"
if block_id=0x98 then print " -- Info for Specific seg"
if block_id=0x9a then print " -- Info for Specific group"
if block_id=0x9c then print " -- Relocation List"
if block_id=0xa0 then print " -- Segment Data"
if block_id=0xa2 then print " -- Duplicated Seg Data"
if block_size<=2 then 2300
if block_id=0x80 then gosub 3000
if block_id=0x88 then gosub 4100
if block_id=0x8a then gosub 3900
if block_id=0x8c then gosub 3600
if block_id=0x90 then gosub 3500
if block_id=0x94 then gosub 4000
if block_id=0x96 then gosub 3400
if block_id=0x98 then gosub 3700
if block_id=0x9a then gosub 3800
if block_id=0x9c then gosub 3200
if block_id=0xa0 then gosub 3100
if block_id=0xa2 then gosub 3300
2300 if block_sum<>0 then print " CRC error." else print
page_num=page_num+1
rem if page_num>23 then print "Hit a key to continue ,or Q to quit" : a$=input$(1) : page_num=0 : if cap$(a$)="Q" then 160
return
3000 rem print module name
a$=""
for i=1 to buff[1]
a$=a$+chr$(buff[i+1])
next
m_name$=a$
print " - Module Name:",a$;
page_num=page_num+1
return
3100 rem print segment info
a=buff[1]
b=buff[2]+buff[3]*0x100
print " - Segment",a,",org 0x";hex$(b);
page_num=page_num+1
return
3200 rem print reloation ex
3210 j=0
3220 rel_addr=0
rel_cont=0
a=buff[j+1] and 3
b=buff[j+2]+a*0x100
c=buff[j+1] and 0xc0
if c<>0xc0 then print " - bad relocation list -"; : return
d=buff[j+1] and 8
e=buff[j+1] and 4
a$=""
if d=8 then a$="segment"
if e=4 and d=0 then a$="offset"
if e=4 and d=8 then a$="segment & offset"
d=buff[j+1] and 2
e=buff[j+3] and 0x10
if d=2 then b$="external" else b$=""
if e=0x10 then c$="group" else c$="seg"
f=buff[j+3] and 0x40
g=buff[j+3] and 4
if f=0 then rel_cont=buff[j+6]+buff[j+7]*0x100 else rel_cont=buff[j+5]+buff[j+6]*0x100
if f=0 and g=0 then rel_cont_seg=buff[j+5] else rel_cont_seg=buff[j+4]
print " - Ex:",b$,a$,"data";
if g=0 then print " - store : (seg",rel_cont_seg;") 0x";hex$(rel_cont),"to (seg",buff[j+4];") [0x";hex$(b);"]";
if g=4 and f=0 then print " - (";c$,buff[j+4];") [0x";hex$(b);"] is external symbol",buff[j+5];
if g=4 and f=0x40 then print " - (";c$,buff[j+4];") [0x";hex$(b);"] is load by OS";
if g=0 then h=6 else h=4
if f=0 then h=h+1
j=j+h
page_num=page_num+1
if j+1<block_size and j+1<20 then print : goto 3220
return
3300 rem print duplication ex
a=buff[1]
b=buff[2]+buff[3]*0x100
c=buff[4]+buff[5]*0x100+buff[6]*0x10000+buff[7]*0x1000000
d=buff[8]
print " - Ex: Seg",a,"[0x";hex$(b);"] ,",c,"dup(",e$;
for i=1 to d
print "0x";hex$(buff[8+i]);" ";
next
print ")";
page_num=page_num+1
return
3400 rem print segment symbol
3410 print " - Segment Symbol:",;
3420 if buff[1]=0 then j=2 else j=1
3430 a=buff[j]
for i=1 to a
c$=chr$(buff[i+j])
print c$;
next
page_num=page_num+1
if j+a+1<block_size then print "."; : j=j+a+1 : goto 3430
return
3500 rem print public symbol
3510 segment=buff[2]
3520 j=3
3530 a=buff[j]
b=buff[j+a+1]+buff[j+a+2]*0x100+buff[j+a+3]*0x10000
print " - Public Symbol: Seg",segment,"[0x";hex$(b);"]",;
for i=1 to a
c$=chr$(buff[i+j])
print c$;
next
page_num=page_num+1
if j+a+4<block_size and j+a+4<80 then print : j=j+a+4 : goto 3530
return
3600 rem print external symbol
3610 j=1
ext_sym_no=0
ext_sym_group=ext_sym_group+1
3620 a=buff[j]
ext_sym_no=ext_sym_no+1
print " - External Symbol (";ext_sym_group;")",ext_sym_no;":";
ext_seg_no=ext_seg_no+1
for i=1 to a
c$=chr$(buff[i+j])
print c$;
next
page_num=page_num+1
if j+a+2<block_size and j+a+2<80 then print : j=j+a+2 : goto 3620
return
3700 rem print segment specifics
a=buff[1]
b=a and 0x60
if b=0 then c$="specific segment alignment"
if b=0x20 then c$="BYTE boundary"
if b=0x40 then c$="WORD boundary"
if b=0x60 then c$="PARAGRAPH boundary"
d=buff[2]+buff[3]*0x100
e=buff[4]
seg_info_num=seg_info_num+1
seg_info[seg_info_num]=e
f=a and 8
if f=8 then d$="public" else d$=""
g=a and 4
if g=4 then e$="stack" else e$=""
print " - Segment ",e/2,",total size",d,"(0x";hex$(d);") is",d$,e$,c$;
page_num=page_num+1
return
3800 rem print group specifics
a=buff[1]
b=buff[3]
print " - Symbol number:",a,",Segment number",b;
page_num=page_num+1
return
3900 rem print end message
print " - End Messages:",;
for i=1 to block_size-1
a$="00"+hex$(buff[i])
b$=right$(a$,2)
print b$,;
next
page_num=page_num+1
return
4000 rem print line info
4010 a=buff[2]
4020 j=3
4030 b=buff[j]+buff[j+1]*0x100
c=buff[j+2]+buff[j+3]*0x100
print " - Ex: (seg",a;") [0x";hex$(c);"] - Line",b,"in",m_name$;
j=j+4
page_num=page_num+1
if j<block_size and j<20 then print : goto 4030
return
4100 rem print end message
k=block_size
if k>15 then k=15
print
print " - Messages:",;
for i=1 to k-1
a=buff[i]
a=a and 0xff
a$="00"+hex$(a)
b$=right$(a$,2)
print b$,;
next
page_num=page_num+1
return
--
☆ [Origin:椰林風情] [From: host-58-115-151-138.dynamic] [Login: **] [Post: **]
→
03/20 20:29, , 1F
03/20 20:29, 1F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):
Programming 近期熱門文章
PTT數位生活區 即時熱門文章