0% found this document useful (0 votes)
3 views

Assignment 1

Uploaded by

air15902197881
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment 1

Uploaded by

air15902197881
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Computer Organization and Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Assignment 1

This assignment must be completed and submitted by moodle no later than Tuesday 19th of November
2024, 9am. The assignment must be solved and submitted individually.

Exercise 1

Determine the values of registers %rax, %rbx and %rcx after the execution of the following programs.

1. (10 points)

MOVQ $142, %rax


LEAQ 58(%rax), %rbx
MOVQ %rbx, %rcx
INCQ %rcx
PUSHQ %rcx
PUSHQ %rax
PUSHQ %rbx
MOVQ $150, %rax
POPQ %rcx
POPQ %rbx
POPQ %rax
2. (10 points)

XORQ %rax, %rax


MOVB $0x1A, %al
MOVSWQ %ax, %rbx
ADDL %eax, %ebx
LEAQ -1(%ebx, %ebx, 4), %ecx
PUSHQ %rcx
PUSHQ %rax
POPQ %rdx
PUSHQ %rbx
MOVQ %rdx, %rbx
POPQ %rax
POPQ %rcx

Exercise 2

(25 points)

Write an ASM program that, assuming that an input is received in register %rdi, prints out “Zero”, “Positive
Even”, “Positive Odd”, “Negative Even”, or “Negative Odd”, according to the sign and parity of the input.
You can use a write system call. You may test your program by prepending as a first instruction the following:

MOVQ $number, %rdi


replacing number with different testing input values. Include appropriate comments to your program, explaining
how your program works.

Exercise 3

(30 points)

Assuming that a string is defined in the .data section, containing solely lowercase letters and white spaces,
write an ASM program that converts the string to camel case format, i.e., replacing every white space and
following lower case character, by the corresponding upper case character (removing the white space). You
may use any looping strategy, and assume that the result is stored in a different memory location in the .data
section. Test your program with different input strings, including strings with leading and trailing spaces, and
multiple spaces separating words. Comment your code to explain how it works.

Some examples of inputs and the corresponding expected outputs are the following:

Input Output
"hello world" "helloWorld"
" hello world" "helloWorld"
"hello world " "helloWorld"
"hello world" "helloWorld"
" hello world " "helloWorld"

Exercise 4

(25 points)

Consider the following program:

long f(long n) {
int i = 0;
while (n > 1) {
if (n % 2 == 0) {
n = n / 2;
}
else {
n = n * 3 + 1;
}
i++;
}
return i;
}

Write an ASM program that implements this function, assuming that the input n is received in %rdi, and
the output is written to %rax. Test your program with different values for the input, and include appropriate
comments in your code to explain how it works.

Some examples of inputs and the corresponding expected outputs are the following:
Input Output
1 0
5 5
10 6
15 17
20 7
25 23
30 18

You might also like