程序的机器级表示(三)

Unary and Binary Operations 

unary operations:

This operand can be either a register or a memory location. For example, the instruction incl (%esp) causes the 4-byte element on the top of the stack to be incremented. 

binary operations :

Observe, however, that the source operand is given first and the destination second.  For example, the instruction subl %eax,%edx decrements register %edx by the value in %eax. (It helps to read the instruction as “Subtract %eax from %edx.”) The first operand can be either an immediate value, a register, or a memory location. The second can be either a register or a memory location. As with the movl instruction, however, the two operands cannot both be memory locations. 

Shift Operations and so on:

左移与右移的不同:

There are two names for the left shift instruction: sal and shl. Both have the same effect, filling from the right with zeros. The right shift instructions differ in that sar performs an arithmetic shift (fill with copies of the sign bit), whereas shr performs a logical shift (fill with zeros).

The destination operand of a shift operation can be either a register or a memory location.  

一段运算的例子:

扫描二维码关注公众号,回复: 1557375 查看本文章

注意汇编的第二行, 相当于一个乘法运算,并且:

The assembly code instructions occur in a different order than in the C source code.  

In general, compilers generate code that uses individual registers for multiple program values and moves program values among the registers

Special Arithmetic Operations :

乘法,除法,取余的例子:

 

 

 Control

Condition Codes :

In addition to the integer registers, the CPU maintains a set of single-bit condition code registers describing attributes of the most recent arithmetic or logical opera- tion. These registers can then be tested to perform conditional branches. The most useful condition codes are: 

CF: Carry Flag. The most recent operation generated a carry out of the most significant bit. Used to detect overflow for unsigned operations.

ZF: Zero Flag. The most recent operation yielded zero.

SF: Sign Flag. The most recent operation yielded a negative value.

OF: Overflow Flag. The most recent operation caused a two’s-complement overflow—either negative or positive. 

Comparison and test instructions :

一个简单的例子:

For example, suppose we used one of the add instructions to perform the equivalent of the C assignment t=a+b, where variables a, b, and t are integers. Then the condition codes would be set according to the following C expressions

CF: (unsigned) t < (unsigned) a      Unsigned overflow

ZF: (t == 0)               Zero

SF: (t < 0)               Negative

OF: (a < 0 == b < 0) && (t < 0 != a < 0)  Signed overflow 

The set instruction and its condition:

Accessing the Condition Codes 

Three ways to use condition codes:

Rather than reading the condition codes directly, there are three common ways of using the condition codes:

(1) we can set a single byte to 0 or 1 depending on some combination of the condition codes,

(2) we can conditionally jump to some other part of the program, or

(3) we can conditionally transfer data.  

关于SET的情况:

A set instruction has either one of the eight single-byte register elements (Figure 3.2) or a single-byte memory location as its destination, setting this byte to either 0 or 1.  

举例:

A typical instruction sequence to compute the C expression a < b, where a and b are both of type int, proceeds as follows: 

Jump Instructions and Their Encodings :

These jump destinations are generally indicated in assembly code by a label

All jump instructions:

unconditional : jmp

others are conditional (they either jump or continue executing at the next instruction in the code sequence), 类比SET

jumps的两种编码方法:

There are several different encodings for jumps, but some of the most commonly used ones are PC relative. That is, they encode the difference between the address of the target instruction and the address of the instruction immediately following the jump. These offsets can be encoded using 1, 2, or 4 bytes.

A second encoding method is to give an “absolute” address, using 4 bytes to directly specify the target. The assembler and linker select the appropriate encodings of the jump destinations. 

一个例子,注意对照,相加与颜色

As these examples illustrate, the value of the program counter when perform- ing PC-relative addressing is the address of the instruction following the jump, not that of the jump itself. 

The following shows the disassembled version of the program after linking

The instructions have been relocated to different addresses, but the encodings of the jump targets in lines 1 and 7 remain unchanged. By using a PC-relative encoding of the jump targets, the instructions can be compactly encoded (requiring just 2 bytes), and the object code can be shifted to different positions in memory without alteration

Translating Conditional Branches 

The general form of an if-else statement in C is given by the template :

if (test-expr) 
    then-statement
else
    else-statement

For this general form, the assembly implementation typically adheres to the following form, where we use C syntax to describe the control flow: 

t = test-expr;
if (!t)
    goto false;
then-statement
      goto done;
false:
    else-statement
done:

基于这个模板的一个例子:




猜你喜欢

转载自www.cnblogs.com/geeklove01/p/9157205.html
今日推荐