PowerPoint Presentation
TUT06 – Machine Language II
COMP1411: Introduction to Computer Systems
Copyright By PowCoder代写 加微信 powcoder
Dr. Xianjin XIA
Department of Computing
The Polytechnic University
Spring 2022
These slides are only intended to use internally. Do not publish it anywhere without permission.
Question 1
Suppose that the following C code
int comp(data_t a, data_t b) {
return a COMP b;
Suppose that a is stored in some portion of register %rdi, and b is stored in some portion of register %rsi. The return value is stored in register %al.
For each case below, infer the data type data_t (e.g., char, short, int, long, their unsigned versions) and the comparison operator COMP (e.g., <, >, ==, >=, <=, !=)
Assembly code Data type data_t Comparison operator COMP
Case 1 cmpl %esi, %edi
setl %al ? ?
Case 2 cmpw %si, %di
setge %al ? ?
Case 3 cmpb %sil, %dil
setbe %al ? ?
Question 1 (Answer)
Assembly code Data type data_t Comparison operator COMP
Case 1 cmpl %esi, %edi
Case 2 cmpw %si, %di
Case 3 cmpb %sil, %dil
SetX Condition Description
sete ZF Equal / Zero
setne ~ZF Not Equal / Not Zero
sets SF Negative
setns ~SF Nonnegative
setg ~(SF^OF)&~ZF Greater (Signed)
setge ~(SF^OF) Greater or Equal (Signed)
setl (SF^OF) Less (Signed)
setle (SF^OF)|ZF Less or Equal (Signed)
seta ~CF&~ZF Above (unsigned)
setb CF Below (unsigned)
unsigned char
Variable register
Return val %al
Question 2
In the following C function, the definition of operation OP is missing.
# define OP __________
long arith(long x) {
return x OP 8;
After compilation, we obtain the following assembly code.
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
Suppose that x is stored in register %rdi
(a) What operation is OP?
(b) Annotate the code to explain how it works
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> t = x + 7, t in %rax
register Variable
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> x & x, set conditional codes
==> t = x + 7, t in %rax
==> if (x&x >= 0), t = x
==> t = t >> 3
# define OP __________
long arith(long x) {
return x OP 8;
register Variable
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> x & x, set conditional codes
==> t = x + 7, t in %rax
==> if (x&x >= 0), t = x
==> t = t >> 3 (t = t / 8)
64-bit signed value >>3 value
1…1111 -1 111…11 -1
1…1110 -2 111…11 -1
1…1101 -3 111…11 -1
1…1100 -4 111…11 -1
1…1011 -5 111…11 -1
1…1010 -6 111…11 -1
1…1001 -7 111…11 -1
1…1000 -8 111…11 -1
-7 / 8 = 0
-1 / 8 = 0
-2 / 8 = 0
-8 / 8 = -1
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> x & x, set conditional codes
==> t = x + 7, t in %rax
==> if (x&x >= 0), t = x
==> t = t >> 3 (t = t / 8)
64-bit signed value >>3 value
1…1111 -1 000…00 0
1…1110 -2 000…00 0
1…1101 -3 000…00 0
1…1100 -4 000…00 0
1…1011 -5 000…00 0
1…1010 -6 000…00 0
1…1001 -7 000…00 0
1…1000 -8 111…11 -1
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> x & x, set conditional codes
==> t = x + 7, t in %rax
==> if (x&x >= 0), t = x
==> t = t >> 3 (t = t / 8)
64-bit signed value >>3 value
1…1111 -1 000…00 0
1…1110 -2 000…00 0
1…1101 -3 000…00 0
1…1100 -4 000…00 0
1…1011 -5 000…00 0
1…1010 -6 000…00 0
1…1001 -7 000…00 0
1…1000 -8 111…11 -1
1…11111 (-1)
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> x & x, set conditional codes
==> t = x + 7, t in %rax
==> if (x&x >= 0), t = x
==> t = t >> 3 (t = t / 8)
64-bit signed value >>3 value
1…1111 -1 000…00 0
1…1110 -2 000…00 0
1…1101 -3 000…00 0
1…1100 -4 000…00 0
1…1011 -5 000…00 0
1…1010 -6 000…00 0
1…1001 -7 000…00 0
1…1000 -8 111…11 -1
1…11010 (-6)
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> x & x, set conditional codes
==> t = x + 7, t in %rax
==> if (x&x >= 0), t = x
==> t = t >> 3 (t = t / 8)
64-bit signed value >>3 value
1…1111 -1 000…00 0
1…1110 -2 000…00 0
1…1101 -3 000…00 0
1…1100 -4 000…00 0
1…1011 -5 000…00 0
1…1010 -6 000…00 0
1…1001 -7 000…00 0
1…1000 -8 111…11 -1
1…11001 (-7)
Question 2 (Answer)
leaq 7(%rdi), %rax
testq %rdi, %rdi
cmovns %rdi, %rax
sarq $3, %rax
==> x & x, set conditional codes
==> t = x + 7, t in %rax
==> if (x&x >= 0), t = x
==> t = t >> 3 (t = t / 8)
64-bit signed value >>3 value
1…1111 -1 000…00 0
1…1110 -2 000…00 0
1…1101 -3 000…00 0
1…1100 -4 000…00 0
1…1011 -5 000…00 0
1…1010 -6 000…00 0
1…1001 -7 000…00 0
1…1000 -8 111…11 -1
1…11000 (-8)
Question 3
Convert the following C function to assembly code.
long fun(long a, long b) {
long result = b;
while (b>0) {
result = result * a;
b = b – a;
return result;
Assume that a, b, result are stored in registers %rdi, %rsi, %rax, respectively.
Question 3 (Answer)
long fun(long a, long b) {
long result = b;
while (b>0) {
result = result * a;
b = b – a;
return result;
GOTO Version (jump-to-middle)
long result = b;
goto TEST;
result = result * a;
b = b – a;
if (b>0) goto LOOP;
return result;
Question 3 (Answer)
long fun(long a, long b) {
long result = b;
while (b>0) {
result = result * a;
b = b – a;
return result;
GOTO Version (jump-to-middle)
long result = b;
goto TEST;
result = result * a;
b = b – a;
if (b>0) goto LOOP;
return result;
GOTO Version (do-while)
long result = b;
if (!(b>0)) goto DONE;
result = result * a;
b = b – a;
if (b>0) goto LOOP;
return result;
Question 3 (Answer)
GOTO Version (do-while)
long result = b;
if (!(b>0)) goto DONE;
result = result * a;
b = b – a;
if (b>0) goto LOOP;
return result;
movq %rsi, %rax # result = b
cmpq $0, %rsi # compare b and 0
jng DONE # if (!(b>0)) goto DONE
imulq %rdi, %rax # result = result * a
subq %rdi, %rsi # b = b – a
cmpq $0, %rsi # compare b and 0
jg LOOP # goto LOOP
Variable register
result %rax
Question 3 (Answer)
movq %rsi, %rax # result = b
cmpq $0, %rsi # compare b and 0
jg L3 # if (b>0) goto L3
ret # return result
imulq %rdi, %rax # result = result * a
subq %rdi, %rsi # b = b – a
jmp L2 # goto L2
Variable register
result %rax
long fun(long a, long b) {
long result = b;
while (b>0) {
result = result * a;
b = b – a;
return result;
/docProps/thumbnail.jpeg
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com