- 論壇徽章:
- 0
|
在《深入了解計(jì)算機(jī)系統(tǒng)》(修訂版)第126頁(yè)中有個(gè)例題,給了你一段匯編代碼,要求寫出相應(yīng)的c語(yǔ)言代碼中的某些部分。所給的匯編代
碼為
movl 8(%ebp), %ecx get a
movl 12(%ebp), %esi get b
cmpl %esi, %ecx
setl %al get t1
cmpl %ecx, %esi
setb -1(%ebp) get t2
cmpw %cx, 16(%ebp)
setge -2(%ebp)
movb %cl, %dl
cmpb 16(%ebp), %dl
setne %bl
cmpl %esi, 16(%ebp)
setg -3(%ebp)
testl %ecx, %ecx
setg %dl
addb -1(%ebp), %al
addb -2(%ebp), %al
addb %bl, %al
addb -3(%ebp), %al
addb %dl, %al
movsbl %al, %eax
所給的完整的的c語(yǔ)言代碼為
char ctest(int a,int b,int c)
{
char t1=a<b;(此處要求填出<)
char t2=b<(unsigned)a;(此處要求填出<和unsigned轉(zhuǎn)換)
char t3=(short)c>=(short)a;(此處要求填出>=和兩個(gè)short轉(zhuǎn)換)
char t4=(char)a!=(char)c;(此處要求填出!=和兩個(gè)char轉(zhuǎn)換)
char t5=a>b;(此處要求填出>)
char t6=a>0;(此處要求填出>)
return t1+t2+t3+t4+t5+t6;
}
上面我都填對(duì)了,但后來(lái)想想還有一些概念上的細(xì)節(jié)有些不確定
(1)set指令只針對(duì)單字節(jié)寄存器或者單字節(jié)存儲(chǔ)器,而上面的-1(%ebp),-2(%ebp),-3(%ebp)只是制定了存儲(chǔ)起始位置,具體大小應(yīng)該是由
類型定義決定的。后來(lái)我小改了這個(gè)程序一下,編譯了一下,發(fā)現(xiàn)我的猜測(cè)好像是對(duì)的。。。
(2)cmpw %cx, 16(%ebp)對(duì)應(yīng)char t3=(short)c>=(short)a,似乎在把4字節(jié)的int型a轉(zhuǎn)換為2字節(jié)的short型a時(shí)取了高2字節(jié)%cx,那么為什么取高兩字節(jié)而不是低兩字節(jié)呢?個(gè)人覺(jué)得可能跟這跟機(jī)器的大/小端規(guī)則有關(guān),是否如此呢?
附:修改的c代碼和匯編
c代碼,全部改為int型
int testchar(int a,int b,int c)
{
int t1=a<b;
int t2=b<(unsigned)a;
int t3=(short)c>=(short)a;
int t4=(char)a!=(char)c;
int t5=c>b;
int t6=a>0;
return t1+t2+t3+t4+t5+t6;
}
匯編後
.file "testchar.c"
.text
.globl testchar
.type testchar,@function
testchar:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl 8(%ebp), %eax
cmpl 12(%ebp), %eax
setl %al
movzbl %al, %eax
movl %eax, -4(%ebp)
movl 12(%ebp), %eax
cmpl 8(%ebp), %eax
setb %al
movzbl %al, %eax
movl %eax, -8(%ebp)
movl 16(%ebp), %eax
cmpw 8(%ebp), %ax
setge %al
movzbl %al, %eax
movl %eax, -12(%ebp)
movb 8(%ebp), %al
cmpb 16(%ebp), %al
setne %al
movzbl %al, %eax
movl %eax, -16(%ebp)
movl 16(%ebp), %eax
cmpl 12(%ebp), %eax
setg %al
movzbl %al, %eax
movl %eax, -20(%ebp)
cmpl $0, 8(%ebp)
setg %al
movzbl %al, %eax
movl %eax, -24(%ebp)
movl -8(%ebp), %eax
addl -4(%ebp), %eax
addl -12(%ebp), %eax
addl -16(%ebp), %eax
addl -20(%ebp), %eax
addl -24(%ebp), %eax
leave
ret
.Lfe1:
.size testchar,.Lfe1-testchar
.ident "GCC: (GNU) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)" |
|