⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asm.html

📁 C ++ in action
💻 HTML
字号:
<html>
<head>
	<title>Assembler Digression</title>
    <meta  name="description" content="A look at the assembly output of a compiler">
    <meta name="keywords" content="assemply, performance, algorithm, efficient,">
	<link rel="stylesheet" href="../../rs.css">
</head>

<body background="../../images/margin.gif" bgcolor="#FFFFDC">

<!-- Main Table -->
<table cellpadding="6">
    <tr>
    <td width="78">
	&nbsp;
    <td>

<h4>Assembler Digression</h4>


<p>&quot;Beautiful it is not, but who cares, if it抯 more efficient.&quot; Well, not really. For those of you who understand the x86 assembler, here抯 the output of the optimizing 16-bit compiler for the two implementations
<p>
<!-- Code -->
<table border="1" cellpadding="8">
<tr>
<th>Index
<th>Pointer
<tr>
	<td class=codeTable>
<pre>?StrLen@@YAHPAD@Z      PROC NEAR
      push      bp
      mov      bp,sp
      <b>push</b>     <b>di</b>
;     pStr = 4
;     register bx = i
      mov      di,WORD PTR [bp+4]
      xor      bx,bx
      <b>cmp</b>      <b>BYTE PTR [di],bl</b>
      <b>je      $FB1596</b>
$F1594:
      inc      bx
      cmp      BYTE PTR <b>[bx][di]</b>,0
      jne      $F1594
$FB1596:
      mov      ax,bx
      <b>pop</b>      <b>di</b>

      mov      sp,bp
      pop      bp
      ret      
?StrLen@@YAHPAD@Z      ENDP</pre>

	<td class=codeTable>

<pre>?StrLen@@YAHPAD@Z      PROC NEAR
      push     bp
      mov      bp,sp

;     register bx = p
;     pStr = 4
      mov      dx,WORD PTR [bp+4]
      mov      bx,dx


$FC1603:
      inc      bx
      cmp      BYTE PTR <b>[bx-1]</b>,0
      jne      $FC1603

      mov      ax,bx
      <b>sub</b>     <b>ax,dx</b>
      <b>dec</b>     <b>ax</b>
      mov      sp,bp
      pop      bp
      ret      
?StrLen@@YAHPAD@Z      ENDP</pre>
</table>
<!-- End Code -->

<p>In the first implementation the compiler decided to use two register variables, hence additional push and pop. The loop is essentially the same, only the addressing mode is different. Under close scrutiny it turns out that  that the instruction in the second loop is longer by one byte in comparison with the first one.

<font face="Courier">
<!-- Code --><table cellpadding="10">
<tr>
<td class=codetable>80 39 00           <td class=codetable>cmp      <td class=codetable>BYTE PTR [bx][di],0  <td class=codetable>; first loop
<tr>
<td class=codetable>80 7f ff 00        <td class=codetable>cmp      <td class=codetable>BYTE PTR [bx-1],0    <td class=codetable>; second loop
</table><!-- End Code -->
</font>

<p>So for really long strings the index implementation beats the pointers. Or does it? A lot depends on the alignment of the instructions. On my old machine, an 80486,  the second loop turned out to be better aligned and therefore produced faster code.
<p>In the pointer implementation some additional pointer arithmetic is done at the end梚n the index implementation, a test is done before entering the loop梑ut then the loop is executed one fewer time. Again, on my machine, the overhead of the index solution turned out to be smaller than the overhead of the pointer one, therefore for strings of up to 3 characters indexes beat pointers.
<p>Frankly, it抯 six of one, half a dozen of another. Is it worth the complication? Have you actually done the comparison of assembly instructions and timings for all your favorite tricks? Maybe it抯 time to throw away all these idioms from the great era of hacking in C and learn some new tricks that are focused on the understandability and maintainability of code. You don抰 want to end up penny-wise but pound-foolish.
<!-- Definition -->
<p>
<table border=4 cellpadding=10><tr>
	<td bgcolor="#ffffff" class=deftable>

Don抰 use pointers where an index will do.</table>
<!-- End Definition -->

</table>
<!-- End Main Table -->
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -