Midterm Answers
Midterm Answers
Midterm Answers
–
FALL
2010
MIDTERM
1
ANSWERS
Question
1
(15
points)
:
Write
a
procedure
is_little_endian
that
will
return
1
when
compiled
and
run
on
a
little-‐-‐-‐
endian
machine,
and
will
return
0
when
compiled
and
run
on
a
big-‐-‐-‐endian
machine.
This
program
should
run
on
any
machine,
regardless
of
its
word
size.
You
may
write
the
procedure
in
C
or
in
Assembly
language.
Answer:
.globl
is
little_endian
_is
little_endian:
pushl
%ebp
movl
%esp,%ebp
movl
$1,-‐4(%ebp)
movb
-‐4(%ebp),%bl
movl
$0,%eax
andl
%bl,%bl
jz
out
movl
$1,
%eax
out:
popl
%ebp
ret
Question
2
(10
points)
:
Assume
we
are
running
code
on
a
10-‐bit
machine
using
two's
complement
arithmetic
for
signed
integers.
A
"short"
integer
is
encoded
using
5
bits.
Fill
in
the
empty
boxes
in
the
table
below.
The
following
definitions
are
used
in
the
table.
short
sy=
-‐15;
int
y
=
sy;
int
x
=
-‐117;
unsigned
ux
=
x;
Note
:
You
will
not
fill
in
entries
marked
with
"-‐".
*
M:
The
value
of
the
significant.
This
should
be
a
number
of
the
form
x
or
x
÷
y,
,
where
x
is
an
integer,
and
y
is
an
integral
power
of
2.
Examples
include
0,
3
÷
4.
Note: you need not fill in entries marked with ”—”.
}
.globl
_reverseString
_reverseString:
pushl
%ebp
movl
%esp,%ebp
//TODO
//
Complete
the
function
ret
movl
8(%ebp),%ebx
xorl
%edi
,
%edi
L1:
cmpb
$0,(ebx,%edi,1)
je
F1
incl
%edi
jmp
L1
F1:
decl
%edi
//
edi
now
points
to
the
last
element
of
array
xorl
%esi,%esi
L2:
movb
(%ebx,%esi,1),
%cl
movb
(%ebx,%edi,1),
%dl
movb
%dl,
(%ebx,%esi,1)
movb
%cl,
(%ebx,%esi,1)
incl
%esi
decl
%edi
cmpl
%esi,%edi
jg
L2
popl
%ebp
ret
Question
5
(10
points)
:
Assume
you
are
an
Intel
X86
processor
and
execute
the
following
program
segment.
Before
the
program
is
started
your
registers
contain
the
following
values.
%EAX
:
0x00000000
%EBX
:
0xFFFFFFFF
%ECX
:
0xFFFFFFFE
%EDX
:
0x00000000
%ESP
:
0xFF0B4400
What
will
be
the
register
contents
upon
the
execution
of
the
each
program
segment
given
below?
PUSHL
%EBX
PUSHL
%EAX
MOVL
%EAX,
%EBX
ADDL
$0XFFFFFFFF,
%EAX
Answer
:
EAX
:
0XFFFFFFFF
EBX
:
0x00000000
ECX
:
0xFFFFFFFE
EDX
:
0x00000000
ESP
:
0xFF0B43F8
Question
6
(15
points
)
:
Answer
the
question
given
in
the
linked
image.
You
can
write
your
answer
in
the
answer
box.
Answer
:
}
Question
7
(
10
points)
Given
the
following
C
function.
Write
the
assembly
equivalent
of
the
code.
else
return(0);
}
Answer:
.globl
_isgreater
_isgreater:
pushl
%ebp
movl
%esp,%ebp
xorl
%eax,%eax
movl
8(%ebp),%ebx
movl
12(%ebp),%edx
cmpl
%ebx,%edx
jle
done
movl
$1,%eax
done:
popl
%ebp
ret
Question
8
(10
points)
Given
the
following
C
function.
Write
the
assembly
equivalent
of
the
code.
Answer
:
.globl
_addNumbers
_addNumbers:
pushl
%ebp
movl
%esp,%ebp
movl
8(%ebp),%ebx
xorl
%esi,%esi
xorl
%eax,%eax
bak:
addl
%esi,%eax
incl
%esi
cmpl
%esi,%ebx
jne
bak
popl
%ebp
ret