0% found this document useful (0 votes)
19 views14 pages

Extra

Uploaded by

ysfnlyzc61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

Extra

Uploaded by

ysfnlyzc61
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Problem 3 (40 pts)

Implement a complete GUI program in Qt that will have the following widgets:
● A line edit widget to enter words
● Two list box widgets that will store list of words
● An add button that will add the word entered in the line edit widget to the list box selected
by the radio buttons left and right.
● Two radio buttons left and right which will indicate to which list to add the word.
● A remove button that will remove a selected word from a list box
● An OK button that will terminate the program.
● A left pointer button ( ฀ ) and a right pointer button ( ฀ ) that will transfer a selected word
in the list box to the other.

A picture of the GUI you will develop is given below.

Problem 4 (16 pts)


Consider the following Qt program which is implemented in three files : myobj.h,
myobj.cpp and main.cpp. What is the output of this program ? Write your answer in
the empty place in the right box below:

[bp + di], al

inc si

al, [bp + si - 1]

jnz loop1

mov dl, ‘y’


*
Problem*4*(22*pts)*
Using&QT&Designer,!develop!!a!dialog!class!called!MyForm.!MyForm!!will!look!like!the!following:!
!

!
!
!MyForm!will!ask!for!a!word!to!be!entered!.!!When!the!left!!(right)!add!button!is!pressed,!it!will!insert!it!to!
the!left!(right)!!wordlist.!!When!the!delete!button!is!pressed!it!should!delete!selected!word.!!When!the!
move!button!is!pressed,!it!will!move!the!selected!word!to!the!other!wordlist.!!Please!use!the!following!
variable!names!when!writing!your!code:!
• left_add_button, right_add_button, delete_button, move_button,
close_button, left_wordlist, right_wordlist, line_edit
*
*
Problem*5*(20*pts)*
The!following!code!implements!the!insertion!sort!in!A86!assembly!language.!!The!program!reads!characters!
until!space!character!is!entered.!It!then!sorts!the!input!characters!and!prints!them.!!Fill!in!the!missing!
entries!in!the!program.!

code segment
mov bp,arry
mov ah,01h
mov cx,0h
input:
int 21h
cmp al,' '
je _______ /* (a) */
mov [bp],al
inc bp
inc cx
jmp input
befloop1:
mov ah,______ /* (b) */
mov bp,_______ /* (c) */
mov si,0h
loop1:
cmp si,cx
je endloop1
mov dl,_______ /* (d) */
mov di,si
loop2:
cmp di,0h
je _________ /* (e) */
mov dh,________ /* (f) */
cmp dh,dl
jle endloop2
mov ______,dh /* (g) */

A86 Assembly insertion sort dec di


jmp loop2
code segment endloop2:
mov bp, arry ; Set BP to the start of the array mov [bp+di],dl
____ si /* (h) */
mov ah, 01h ; Prepare to read characters jmp loop1
mov cx, 0h ; Initialize counter endloop1:
input: cmp _____,0h /* (i) */
int 21h ; Read a character je finish
cmp al, ' ' ; Check if the character is a space mov dl,[bp]
je befloop1 ; (a) If yes, jump to the sorting section int 21h ;
dec cx
mov [bp], al ; Store the character in the array
inc ______ /* (j) */
inc bp ; Move to the next position in the array jmp endloop1
inc cx ; Increment the counter finish:
jmp input ; Continue reading characters int 20h
arry:
befloop1: db ?
mov ah, 02h ; (b) Prepare to print characters code ends
mov bp, arry ; (c) Reset BP to the start of the array
mov si, 0h ; Initialize the outer loop index
!
!
loop1: !
cmp si, cx ; Compare index with count of characters
je endloop1 ; If all characters are sorted, exit outer loop
mov dl, [bp + si] ; (d) Load the current character into DL
mov di, si ; Copy outer loop index to inner loop index

loop2:
cmp di, 0h ; Compare inner loop index with 0
je endloop2 ; (e) If inner loop index reaches 0, exit inner loop
mov dh, [bp + di - 1] ; (f) Load the previous character into DH
cmp dh, dl ; Compare the previous character with the current character
jle endloop2 ; If the previous character is less than or equal, exit inner loop
mov [bp + di], dh ; (g) Move the previous character to the current position
dec di ; Decrement the inner loop index
jmp loop2 ; Repeat inner loop

endloop2:
mov [bp + di], dl ; Place the current character in its correct position
inc si ; (h) Increment the outer loop index
jmp loop1 ; Repeat outer loop

endloop1:
cmp cx, 0h ; (i) Check if there are still characters to print
je finish ; If all characters are printed, finish
mov dl, [bp] ; Load the first character into DL
int 21h ; Print the character
dec cx ; Decrement the character counter
inc bp ; (j) Move to the next character in the array
jmp endloop1 ; Repeat printing loop

finish:
int 20h ; Terminate program
arry:
db ? ; Define a 100-byte array for characters
code ends
• mul cx: Multiplies ax by 10. Assembly Code Explanation
• add ax, bp: Adds bp to ax. Initialization and Reading Input
• mov cx, ax: Moves the result to cx.
; read a
• jmp morechar: Jumps back to read the next character. call myread
mov va, cx
• myret:: Label for the end of the subroutine.
• ret: Returns from the subroutine. • call myread: Calls the subroutine myread which reads a non-negative integer from the user and stores it in the cx
register.

Subroutine myprint • mov va, cx: Moves the value in the cx register to the memory location va.

myprint:
mov si, 10d Loop (While 100 - a)
xor dx, dx
push ’ ’ ; push newline labl1:
mov cx, 1d ; while (100 - a)
nonzero: push 100d
div si push va
add dx, 48d pop cx
push dx pop ax
inc cx sub ax, cx
xor dx, dx push ax
cmp ax, 0h pop ax
jne nonzero cmp ax, 0
writeloop: jz labl2
pop dx
mov ah, 02h
int 21h • labl1:: Label marking the start of the loop.
dec cx
jnz writeloop • push 100d: Pushes the constant 100 onto the stack.
ret
• push va: Pushes the value of va (which is a) onto the stack.
• mov si, 10d: Moves 10 to si.
• pop cx: Pops the top value of the stack (which is a) into cx.
• xor dx, dx: Clears dx.
• pop ax: Pops the next value of the stack (which is 100) into ax.
• push ’ ’: Pushes a space (newline).
• sub ax, cx: Subtracts cx (which is a) from ax (which is 100), resulting in 100 - a.
• mov cx, 1d: Moves 1 to cx.
• push ax: Pushes the result back onto the stack.
• nonzero:: Label for the loop to convert number to ASCII.
• pop ax: Pops the result into ax.
• div si: Divides ax by 10.
• cmp ax, 0: Compares ax with 0.
• add dx, 48d: Converts remainder to ASCII.
• jz labl2: Jumps to label labl2 if ax is zero (i.e., if 100 - a is zero).
• push dx: Pushes the ASCII character.
• inc cx: Increments cx. Begin Loop Body
• xor dx, dx: Clears dx. ; begin
push va
• cmp ax, 0h: Compares ax with 0. pop ax
call myprint
• jne nonzero: Jumps to nonzero if ax is not 0. push offset va
push va
• writeloop:: Label for the loop to print characters. push 1
pop cx
• pop dx: Pops a character to dx. pop ax
add ax, cx
• mov ah, 02h: Prepares for character output. push ax
pop ax
• int 21h: Interrupts to DOS to print the character. pop bp
mov [bp], ax
• dec cx: Decrements cx. ; a = a + 1
jmp labl1 ; jump to while beginning
• jnz writeloop: Jumps to writeloop if cx is not 0.
• ret: Returns from the subroutine. • push va: Pushes the value of va (which is a) onto the stack.
• pop ax: Pops the value into ax.
Variables
• call myprint: Calls the subroutine myprint which prints the value in ax.
va dw ?
• push offset va: Pushes the offset address of va onto the stack.
• va dw ?: Declares a variable va which is a word (16 bits) and uninitialized. • push va: Pushes the value of va onto the stack again.

3 1

Full Code • push 1: Pushes the constant 1 onto the stack.

; read a • pop cx: Pops the 1 into cx.


call myread
mov va, cx • pop ax: Pops the value of va into ax.
labl1: • add ax, cx: Adds cx (which is 1) to ax (which is a), resulting in a + 1.
; while (100 - a)
push 100d • push ax: Pushes the result back onto the stack.
push va
pop cx • pop ax: Pops the result into ax.
pop ax
sub ax, cx • pop bp: Pops the offset address of va into bp.
push ax
pop ax
• mov [bp], ax: Moves the new value of a into the memory location va.
cmp ax, 0
jz labl2
• jmp labl1: Jumps back to the beginning of the loop.
; begin
push va
pop ax
Exit
call myprint
push offset va labl2:
push va ; exit to DOS
push 1 int 20h
pop cx
pop ax
add ax, cx • labl2:: Label marking the end of the loop.
push ax
pop ax • int 20h: Interrupts to DOS to terminate the program.
pop bp
mov [bp], ax
; a = a + 1 Subroutine myread
jmp labl1 ; jump to while beginning
myread:
labl2: MOV CX, 0
; exit to DOS morechar:
int 20h mov ah, 01h
int 21h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; mov dx, 0
; reads a nonnegative integer and puts mov dl, al
; it in cx register mov ax, cx
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cmp dl, 0D
myread: je myret
MOV CX, 0 sub dx, 48d
morechar: mov bp, dx
mov ah, 01h mov ax, cx
int 21h mov cx, 10d
mov dx, 0 mul cx
mov dl, al add ax, bp
mov ax, cx mov cx, ax
cmp dl, 0D jmp morechar
je myret myret:
sub dx, 48d ret
mov bp, dx
mov ax, cx
mov cx, 10d • MOV CX, 0: Initializes cx to 0.
mul cx
add ax, bp • morechar:: Label for the loop to read each character.
mov cx, ax
jmp morechar • mov ah, 01h: Prepares for a character input.
myret:
ret • int 21h: Interrupts to DOS to read a character.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
• mov dx, 0: Clears dx.
; number in AX register is printed
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
myprint:
• mov dl, al: Moves the input character to dl.
mov si, 10d
xor dx, dx • mov ax, cx: Moves cx to ax.
push ’ ’ ; push newline
mov cx, 1d • cmp dl, 0D: Compares dl with carriage return (Enter key).
nonzero:
div si • je myret: Jumps to myret if Enter key is pressed.
add dx, 48d
push dx • sub dx, 48d: Converts ASCII to decimal.
inc cx
xor dx, dx • mov bp, dx: Moves the result to bp.
cmp ax, 0h
jne nonzero • mov ax, cx: Moves cx to ax.
writeloop:
pop dx • mov cx, 10d: Moves 10 to cx.
5 section .text mov ah, 02h
int 21h
6 global _start dec cx
jnz writeloop
- The .data section declares and initializes the variables a and i. - The .text section declares the ret
start of the code and makes start the entry point.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Variables are put in this area
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Initialization va dw ?

1 _start :
2 mov dword [ a ] , 1 ; a = 1
3 mov dword [ i ] , 1 ; i = 1

- mov dword [a], 1: Sets a to 1. - mov dword [i], 1: Sets i to 1.

Loop Start
1 loop_start :
2 mov eax , [ i ]
3 cmp eax , 2 ; compare i with 2
4 jg loop_end ; if i > 2 , exit loop

- mov eax, [i]: Loads the value of i into eax. - cmp eax, 2: Compares i with 2. - jg loop end:
Jumps to loop end if i is greater than 2.

Expression Calculation
1 mov eax , [ a ] ; eax = a
2 add eax , 2 ; eax = 2 + a
3 mov ebx , [ a ] ; ebx = a
4 imul eax , ebx ; eax = (2 + a ) * a
5 add eax , [ a ] ; eax = (2 + a ) * a + a
6 mov [ a ] , eax ; a = (2 + a ) * a + a

- mov eax, [a]: Loads the value of a into eax. - add eax, 2: Adds 2 to a, storing the result in eax.
- mov ebx, [a]: Loads the value of a into ebx. - imul eax, ebx: Multiplies eax by ebx, storing the
result in eax. - add eax, [a]: Adds the original value of a to eax. - mov [a], eax: Stores the result
back into a.

Increment and Loop


1 add dword [ i ] , 1 ; i = i + 1
2 jmp loop_start ; repeat the loop

- add dword [i], 1: Increments i by 1. - jmp loop start: Jumps back to the start of the loop.

Loop End
1 loop_end :
2 mov eax , 1 ; exit code
3 int 0 x80 ; call kernel

- mov eax, 1: Sets the exit code. - int 0x80: Calls the kernel to exit the program.
This detailed analysis helps in understanding how the assembly instructions correspond to the C code
and the overall flow of the program.

2 5

2007 - Problem 9 (20 pts) 2023 - Problem 3


(a) Explain what the following A86 assembly language program does. Consider the following statement:
(b) Suppose the following input is given to the program: a = (2 + a) · a + a
1 hello (a) Write down the postfix expression for the statement. (b) Write down the A86 assembly code that
(Note that is the blank character.) Answer the following questions: can be generated for the following program:
1 a = 1;
(i) What are the contents of cl, dl, al, [bx], and [bp] at the point the program reaches the point 2 for ( i = 1; i <= 2; i ++) {
marked as ; here? 3 a = (2 + a ) * a + a ;
4 }
(ii) What will be the output of the program for this input?

1 code segment
2 mov bp , arry
Answer
3 mov bx , arry
4 mov ah , 01 h (a) The postfix expression for the statement a = (2 + a) * a + a is:
5 input :
6 int 21 h addr-a 2 val-a + val-a ∗ a + =
7 cmp al , ’ ’
8 je testword (b) Here is the A86 assembly code for the program:
9 mov [ bp ] , al
1 section .data
10 inc bp
2 a dd 1
11 jmp input
3 i dd 0
12 testword :
4
13 dec bp
5 section .text
14 mov dl , ’n ’
6 global _start
15 begloop :
7
16 cmp bx , bp
8 _start :
17 jae yesitis
9 mov dword [ a ] , 1 ; a = 1
18 mov cl , [ bx ]
10 mov dword [ i ] , 1 ; i = 1
19 cmp cl , [ bp ]
11
20 jne notitisn
12 loop_start :
21 inc bx
13 mov eax , [ i ]
22 dec bp
14 cmp eax , 2 ; compare i with 2
23 ; here
15 jg loop_end ; if i > 2 , exit loop
24 jmp begloop
16
25 yesitis :
17 mov eax , [ a ] ; eax = a
26 mov dl , ’y ’
18 add eax , 2 ; eax = 2 + a
27 notitisn :
19 mov ebx , [ a ] ; ebx = a
28 mov ah , 02 h
20 imul eax , ebx ; eax = (2 + a ) * a
29 int 21 h
21 add eax , [ a ] ; eax = (2 + a ) * a + a
30 finish :
22 mov [ a ] , eax ; a = (2 + a ) * a + a
31 int 20 h
23
32 arry :
24 add dword [ i ] , 1 ; i = i + 1
33 db ?
25 jmp loop_start ; repeat the loop
34 code ends
26

27 loop_end :
28 mov eax , 1 ; exit code
Answer 29 int 0 x80 ; call kernel

(a) The program reads characters from the input until a blank space is encountered. It stores the characters
in an array. Once a blank space is found, it compares the characters from the start and end of the array Lecture: Understanding Problem 3
towards the center to check if the word is a palindrome. If the word is a palindrome, it prints ’y’; otherwise,
it prints ’n’. In this lecture, we will analyze the given C code segment and its corresponding A86 assembly code.
(b) Analysis for the input "hello ":
Section Declarations
(i) Contents of registers and memory at the point marked ; here:
1 section .data
1. Initial Values: 2 a dd 1
3 i dd 0
• bp and bx point to the start of the array arry. 4
2001 - Problem 5 • Characters ’h’, ’e’, ’l’, ’l’, ’o’ are stored in the array.

The following C program was compiled by gcc to generate GNU assembler code. 2. After input loop:
1 # include < stdio .h > • bp points to the last character ’o’ in the array.
2
3 main () { • The array arry contains: [’h’, ’e’, ’l’, ’l’, ’o’].
4 int i ;
5 int a [4]; 3. During palindrome check:
6
7 for ( i =3; i >= 0; i - -) { • mov cl, [bx]: loads cl with the character at bx (start of the array).
8 a[i] = i;
9 } • cmp cl, [bp]: compares cl with the character at bp (end of the array).
10
11 printf ( " % d % d % d % d \ n " , a [2] , a [1] , a [3] , a [0]) ; At the point marked ; here:
12 }
• cl = ’h’
The GNU assembler code is given below. Fill in the blanks:
• dl = ’n’ (initially set to ’n’, indicating not a palindrome)
1 .file " ex.c "
2 .version " 01 .01 " • al = last input character (blank space)
3 gcc2_compiled. :
• [bx] = ’h’ (first character in the array)
4 .section .rodata
5 .LC0 : • [bp] = ’o’ (last character in the array)
6 .string " % d % d % d % d \ n "
7 .text
8 .align 4 (ii) Output of the Program for Input "hello ":
9 .globl main
10 .type main , @function The program compares ’h’ with ’o’, finds that they are not equal, and sets dl to ’n’. Thus, the output of
11 main : the program will be ’n’.
12 pushl % ebp
13 movl % esp , % ebp
14 subl $24 , % esp
15 leal -16(% ebp ) , % eax
16 .p2align 4 , ,7
17 .L21 :
18 movl % ecx ,(% eax ,% ecx ,4) /* (a , b ) */
19 decl % ecx
20 jns .L21
21 movl -16(% ebp ) ,% eax
22 addl $ -12 ,% esp
23 pushl % eax
24 movl -12(% ebp ) ,% eax /* ( c ) */
25 pushl % eax
26 movl -8(% ebp ) ,% eax /* ( d ) */
27 pushl % eax
28 movl -4(% ebp ) ,% eax /* ( e ) */
29 pushl % eax
30 pushl $.LC0
31 call printf
32 movl % ebp ,% esp
33 popl % ebp
34 ret
35 .Lfe1 :
36 .size main , .Lfe1 - main

Answer
Here is the filled-in GNU assembler code:
1 .file " ex.c "
2 .version " 01 .01 "
3 gcc2_compiled. :
4 .section .rodata
5 .LC0 :

1 2

6 .string " % d % d % d % d \ n " 2001 Final - Problem 5 (15 pts)


7 .text
8 .align 4 Answer the following questions briefly:
9 .globl main
10 .type main , @function
11 main : (a)
12 pushl % ebp
13 movl % esp , % ebp What is the contents of the DX register after instruction 4 is executed?
14 subl $24 , % esp
1 code segment
15 leal -16(% ebp ) , % eax
2 MOV SI ,4 d
16 .p2align 4 , ,7
3 MOV DX ,10 h
17 .L21 :
4 MOV AX ,10 h
18 movl % ecx ,(% eax ,% ecx ,4) /* ( a ) */
5 DIV SI ; instruction 4
19 decl % ecx
6 INT 20 h
20 jns .L21
7 code ends
21 movl -16(% ebp ) ,% eax
22 addl $ -12 ,% esp Answer: The contents of the DX register after instruction 4 is executed are 0.
23 pushl % eax
24 movl -12(% ebp ) ,% eax /* ( c ) */
25 pushl % eax (b)
26 movl -8(% ebp ) ,% eax /* ( d ) */
27 pushl % eax Can segments overlap? Explain.
28 movl -4(% ebp ) ,% eax /* ( e ) */ Answer: Yes, segments can overlap. Segment overlapping occurs when two or more segments share
29 pushl % eax some of their memory addresses. This can happen when the segment registers (CS, DS, SS, ES) are set such
30 pushl $.LC0
31 call printf that their base addresses plus the offset addresses point to overlapping memory locations. Overlapping
32 movl % ebp ,% esp segments can be useful for certain programming techniques but can also lead to errors if not managed
33 popl % ebp properly.
34 ret
35 .Lfe1 :
36 .size main , .Lfe1 - main (c)
Give an example of an A86 instruction that has register-indirect addressing mode.
Answer: An example of an A86 instruction that has register-indirect addressing mode is MOV AX,
Lecture: Understanding Problem 5 [BX]. This instruction moves the data from the memory location pointed to by the BX register into the
AX register.
In this lecture, we will analyze the given C code segment and its corresponding GNU assembler code.

(d)
Function Prologue
What is the disadvantage of immediate addressing mode?
1 pushl % ebp Answer: The disadvantage of immediate addressing mode is that the immediate data is encoded
2 movl % esp , % ebp
3 subl $24 , % esp
directly in the instruction, making the instruction longer. This can increase the size of the code and may
4 leal -16(% ebp ) , % eax not be efficient in terms of memory usage. Additionally, immediate data cannot be changed at runtime,
limiting flexibility.
- These instructions set up the stack frame for the function. - pushl %ebp saves the base pointer of the
calling function. - movl %esp, %ebp sets the base pointer to the current stack pointer. - subl $24, %esp
allocates 24 bytes of space on the stack for local variables. - leal -16(%ebp), %eax loads the address of (e)
a into eax. Write down three instructions that will swap the contents of registers AX and BX without making any
memory reference(s) and without using any extra register.
Loop to Initialize Array Answer: Three instructions that will swap the contents of registers AX and BX without making any
memory references and without using any extra register are:
1 .p2align 4 , ,7
1 XOR AX , BX
2 .L21 :
2 XOR BX , AX
3 movl % ecx ,(% eax ,% ecx ,4) /* ( a ) */
3 XOR AX , BX
4 decl % ecx
5 jns .L21

- The loop runs from i = 3 to i = 0. - movl %ecx,(%eax,%ecx,4) stores the value of ecx (which is
the index i) into the array a[i]. - decl %ecx decrements ecx by 1. - jns .L21 jumps to .L21 if the
result of the decrement is non-negative (i.e., i >= 0).
14 compare : Function Epilogue
15 mov al ,[ bp + si ] ; /* ( b ) */
16 mov al ,[ bx + si -1] 1 movl -16(% ebp ) ,% eax
17 cmp al ,[ bx + si -1] ; /* ( c ) */ 2 addl $ -12 ,% esp
18 jne nextpos 3 pushl % eax
19 cmp si ,3 d 4 movl -12(% ebp ) ,% eax /* ( c ) */
20 jne compare 5 pushl % eax
21 jmp found 6 movl -8(% ebp ) ,% eax /* ( d ) */
22 nextpos : 7 pushl % eax
23 inc bp 8 movl -4(% ebp ) ,% eax /* ( e ) */
24 dec di 9 pushl % eax
25 cmp di , 0 ; /* ( d ) */ 10 pushl $.LC0
26 jne loop1 11 call printf
27 notfound : 12 movl % ebp ,% esp
28 mov dl , ’n ’ 13 popl % ebp
29 jmp pprint 14 ret
30 found : 15 .Lfe1 :
31 mov dl , ’y ’ ; /* ( e ) */ 16 .size main , .Lfe1 - main
32 pprint :
33 mov ah ,02 h - movl -16(%ebp), %eax loads a[2] into eax. - pushl %eax pushes a[2] onto the stack. - movl
34 int 21 h -12(%ebp), %eax loads a[1] into eax. - pushl %eax pushes a[1] onto the stack. - movl -8(%ebp),
35 int 20 h %eax loads a[3] into eax. - pushl %eax pushes a[3] onto the stack. - movl -4(%ebp), %eax loads a[0]
36 ali :
into eax. - pushl %eax pushes a[0] onto the stack. - pushl $LC0 pushes the format string onto the
37 db ’ ali ’
38 iword : stack. - call printf calls the printf function. - movl %ebp, %esp restores the stack pointer. - popl
39 db ? %ebp restores the base pointer. - ret returns from the function.
40 code ends This detailed analysis helps in understanding how the assembler instructions correspond to the C code
and the overall flow of the program.

Lecture: Understanding Problem 4


In this lecture, we will analyze the given A86 assembly code to understand its corresponding functionality.

Setting Up the Registers


1 mov ah ,01 h
2 mov bp , iword
3 mov bx , ali
4 xor di , di

- mov ah,01h: Prepares to read a character from input. - mov bp,iword: Sets bp to point to iword. -
mov bx,ali: Sets bx to point to ali. - xor di,di: Clears di.

Input Loop
1 input :
2 int 21 h
3 mov [ bp + di ] , al ; /* ( a ) */
4 inc di
5 cmp al ,13 d
6 jne input

- int 21h: Reads a character. - mov [bp+di], al: Stores the character in memory at [bp+di]. -
inc di: Increments di to point to the next character position. - cmp al,13d: Compares al with carriage
return (13 in decimal). - jne input: If not carriage return, continue inputting characters.

Search Loop
1 loop1 :
2 mov si ,0
3 compare :

2 3

4 mov al ,[ bp + si ] ; /* ( b ) */ 2001 - Problem 4


5 mov al ,[ bx + si -1]
6 cmp al ,[ bx + si -1] ; /* ( c ) */ The following A86 program inputs a string (terminated by the ’return’ key). It then searches this string
7 jne nextpos to see if ali is a substring of this input string. If it is, it prints y (for yes) and otherwise, it prints n (for
8 cmp si ,3 d
9 jne compare no). Fill in the blanks in the code.
10 jmp found 1 code segment
11 nextpos : 2 mov ah ,01 h
12 inc bp 3 mov bp , iword
13 dec di 4 mov bx , ali
14 cmp di , 0 ; /* ( d ) */ 5 xor di , di
15 jne loop1 6 input :
16 notfound : 7 int 21 h
17 mov dl , ’n ’ 8 mov [ bp + di ] , al ; /* ( a ) */
18 jmp pprint 9 inc di
19 found : 10 cmp al ,13 d
20 mov dl , ’y ’ ; /* ( e ) */ 11 jne input
21 pprint : 12 loop1 :
22 mov ah ,02 h 13 mov si ,0
23 int 21 h 14 compare :
24 int 20 h 15 mov al ,[ bp + si ] ; /* ( b ) */
25 ali : 16 mov al ,[ bx + si -1]
26 db ’ ali ’ 17 cmp al ,[ bx + si -1] ; /* ( c ) */
27 iword : 18 jne nextpos
28 db ? 19 cmp si ,3 d
29 code ends 20 jne compare
21 jmp found
This detailed analysis helps in understanding how the assembly instructions correspond to the func- 22 nextpos :
tionality of checking if ”ali” is a substring of the input string. 23 inc bp
24 dec di
25 cmp di , 0 ; /* ( d ) */
26 jne loop1
27 notfound :
28 mov dl , ’n ’
29 jmp pprint
30 found :
31 mov dl , ’y ’ ; /* ( e ) */
32 pprint :
33 mov ah ,02 h
34 int 21 h
35 int 20 h
36 ali :
37 db ’ ali ’
38 iword :
39 db ?
40 code ends

Answer
Here is the filled-in A86 program:
1 code segment
2 mov ah ,01 h
3 mov bp , iword
4 mov bx , ali
5 xor di , di
6 input :
7 int 21 h
8 mov [ bp + di ] , al ; /* ( a ) */
9 inc di
10 cmp al ,13 d
11 jne input
12 loop1 :
13 mov si ,0
2023 - Problem 4 (20 pts) 2013 mt1 - Problem 3 (35 pts)
The following assembly code was produced by the GNU C compiler by compiling a C program with the Using QT Designer, develop a dialog class called MyDialog. MyDialog will look like the following. It will
gcc -S -m32 command. The C program only had a main function with some variable definitions and ask for a word to be entered. Then, when the button is pressed, it will emit a signal called search which
statements. Only a part of the generated assembly code is shown below: also sends the string entered in the line edit box.
1 _main :
2 LFB1 :
3 pushl % ebp
4 LCFI0 :
5 movl % esp , % ebp
6 LCFI1 :
7 subl $16 , % esp
8 call _ _ _ x 8 6 . g e t _ p c _ t h u n k . a x
9 L1$pb :
10 movl $7 , -4(% ebp )
11 movl $4 , -8(% ebp )
12 movl $5 , -12(% ebp )
13 movl -4(% ebp ) , % eax
14 imull -8(% ebp ) , % eax
15 movl % eax , -4(% ebp )
16 movl -12(% ebp ) , % eax
17 addl % eax , -8(% ebp )
18 movl -4(% ebp ) , % eax
19 addl % eax , -12(% ebp )
20 movl -8(% ebp ) , % eax
21 subl -12(% ebp ) , % eax
22 movl % eax , -4(% ebp )
23 movl $0 , % eax
Answer
24 leave
25 LCFI2 : Here is the C++ code for the required QT program using QT Designer:
26 ret

Give the full C code that corresponds to the above GNU assembly code (in other words, disassemble Step-by-Step Guide
the GNU assembly code given above). 1. Design the UI using QT Designer
1. Open QT Designer and create a new dialog.
Answer
2. Add a QLabel, QLineEdit, and QPushButton to the dialog.
Here is the full C code corresponding to the given assembly code:
3. Set the QLabel text to ”Search:”.
1 # include < stdio .h >
2
4. Set the QPushButton text to ”Search”.
3 int main () {
4 int a , b , c ; 5. Save the UI file as mydialog.ui.
5 a = 7;
6 b = 4;
7 c = 5; 2. Convert the UI file to a header file
8 a = b * a;
9 b = b + c; 1 pyuic5 mydialog . ui -o ui_mydialog . h
10 c = c + a;
11 a = b - c;
12 return 0; 3. Create the C++ implementation
13 }
1. Create a new file named mydialog.h:
1 # ifndef MYDIALOG_H
2 # define MYDIALOG_H
Lecture: Understanding Problem 4 3

4 # include < QDialog >


In this lecture, we will analyze the given assembly code to understand its corresponding C code. 5 # include " ui_mydialog . h "
6
7 class MyDialog : public QDialog {
Function Prologue 8 Q_OBJECT
9

10 public :

1 1

1 _main : 11 explicit MyDialog ( QWidget * parent = nullptr ) ;


2 LFB1 : 12
3 pushl % ebp 13 signals :
4 LCFI0 : 14 void search ( const QString & text ) ;
5 movl % esp , % ebp 15
6 LCFI1 : 16 private slots :
7 subl $16 , % esp 17 void o n _ s e a r c h B u t t o n _ c l i c k e d () ;
8 call _ _ _ x 8 6 . g e t _ p c _ t h u n k . a x 18
19 private :
- These instructions set up the stack frame for the function. - pushl %ebp saves the base pointer of 20 Ui :: MyDialog ui ;
the calling function. - movl %esp, %ebp sets the base pointer to the current stack pointer. - subl $16, 21 };
%esp allocates 16 bytes of space on the stack for local variables. 22
23 # endif // MYDIALOG_H

Initializing Variables 2. Create a new file named mydialog.cpp:


1 L1$pb : 1 # include " mydialog . h "
2 movl $7 , -4(% ebp ) 2

3 movl $4 , -8(% ebp ) 3 MyDialog :: MyDialog ( QWidget * parent ) :


4 movl $5 , -12(% ebp ) 4 QDialog ( parent ) {
5 ui . setupUi ( this ) ;
- movl $7, -4(%ebp) sets a to 7. - movl $4, -8(%ebp) sets b to 4. - movl $5, -12(%ebp) sets c to 6 connect ( ui . searchButton , & QPushButton :: clicked , this , & MyDialog ::
5. on_searchButton_clicked );
7 }
8
First Operation 9 void MyDialog :: o n _ s e a r c h B u t t o n _ c l i c k e d () {
10 QString text = ui . lineEdit - > text () ;
1 movl -4(% ebp ) , % eax 11 emit search ( text ) ;
2 imull -8(% ebp ) , % eax 12 }
3 movl % eax , -4(% ebp )

- Loads a into eax. - Multiplies eax by b. - Stores the result back into a, making it a = b * a. 4. Create the main application file

1 # include < QApplication >


Second Operation 2 # include " mydialog . h "
3
1 movl -12(% ebp ) , % eax 4 int main ( int argc , char * argv []) {
2 addl % eax , -8(% ebp ) 5 QApplication app ( argc , argv ) ;
6
- Loads c into eax. - Adds c to b. - Stores the result back into b, making it b = b + c.
7 MyDialog dialog ;
8 dialog . show () ;
Third Operation 9
10 return app . exec () ;
11 }
1 movl -4(% ebp ) , % eax
2 addl % eax , -12(% ebp ) This detailed guide helps in creating a QT program with a dialog box that emits a signal when a button
- Loads a into eax. - Adds a to c. - Stores the result back into c, making it c = c + a. is clicked.

Fourth Operation
1 movl -8(% ebp ) , % eax
2 subl -12(% ebp ) , % eax
3 movl % eax , -4(% ebp )

- Loads b into eax. - Subtracts c from b. - Stores the result back into a, making it a = b - c.

Function Epilogue
1 movl $0 , % eax
2 leave
3 LCFI2 :
4 ret
3. Changing File Permissions - movl $0, %eax sets the return value to 0. - leave restores the base pointer and stack pointer. - ret
returns from the function.
1 chmod 751 datafile This detailed analysis helps in understanding how the assembly instructions correspond to the C code
- ‘chmod 751‘ sets the permissions to ‘-rwxr-x–x‘ (owner can read, write, and execute; group can read and and the overall flow of the program.
execute; others can execute).

4. Appending Output to a File While Displaying in Terminal


1 prog . exe | tee -a out . txt

- The ‘tee‘ command reads from standard input and writes to standard output and files. The ‘-a‘ option
appends to the specified file.

5. Creating a Static Library


1 ar rcs libveli . a / home / veli / PROJ /*. o

- ‘ar rcs‘ creates an archive (static library) named ‘libveli.a‘ from the object files in the specified directory.

6. Remote Login with Secure Shell


1 ssh veli@mach1 . cmpe . boun . edu . tr

- ‘ssh‘ is used to log into a remote machine securely. The syntax is ‘ssh username@hostname‘.

7. Displaying the Manual Page


1 man gcc

- ‘man‘ displays the manual page for the specified command.

8. Listing All Files Including Hidden Ones


1 ls -a

- The ‘-a‘ option for ‘ls‘ lists all files, including hidden ones (those starting with a dot).

9. Killing a Process by ID
1 kill -9 321

- ‘kill -9‘ forcefully terminates the process with the specified process ID (321).
This lecture provides a detailed understanding of the Linux commands required for Problem 1. Practice
these commands to become more familiar with their usage.

2 3

2023 - Problem 1 (18 pts) 2023 - Problem 1 (18 pts)


Give the Linux commands that will do the following: Give the Linux commands that will do the following:

1. Execute the program prog.exe and send its error messages to the null device. 1. Execute the program prog.exe and send its error messages to the null device.

2. Execute the program called prog.exe in the background. 2. Execute the program called prog.exe in the background.

3. Assign the permission mode "-rwxr-x--x" to the file datafile. 3. Assign the permission mode "-rwxr-x--x" to the file datafile.

4. Execute the program prog.exe and append its output to the file out.txt while also showing the 4. Execute the program prog.exe and append its output to the file out.txt while also showing the
output in the terminal. output in the terminal.

5. Create a static library file libveli.a using the object files in the directory /home/veli/PROJ. 5. Create a static library file libveli.a using the object files in the directory /home/veli/PROJ.

6. Do remote login to a computer named mach1.cmpe.boun.edu.tr with secure shell (and using user- 6. Do remote login to a computer named mach1.cmpe.boun.edu.tr with secure shell (and using user-
name veli). name veli).

7. Display the manual page for gcc. 7. Display the manual page for gcc.

8. List all files including the hidden ones. 8. List all files including the hidden ones.

9. Kill the process numbered 321. 9. Kill the process numbered 321.

Answer Answer
1. prog.exe 2> /dev/null 1. prog.exe 2> /dev/null

2. ./prog.exe & 2. ./prog.exe &

3. chmod 751 datafile 3. chmod 751 datafile

4. prog.exe | tee -a out.txt 4. prog.exe | tee -a out.txt

5. ar rcs libveli.a /home/veli/PROJ/*.o 5. ar rcs libveli.a /home/veli/PROJ/*.o

6. ssh [email protected] 6. ssh [email protected]

7. man gcc 7. man gcc

8. ls -a 8. ls -a

9. kill -9 321 9. kill -9 321

Lecture: Understanding Problem 1 Lecture: Understanding Problem 1


In this lecture, we will go through each of the Linux commands mentioned in Problem 1 in detail. In this lecture, we will go through each of the Linux commands mentioned in Problem 1 in detail.

1. Redirecting Error Messages to Null Device 1. Redirecting Error Messages to Null Device
1 prog . exe 2 > / dev / null 1 prog . exe 2 > / dev / null

- The ‘2¿‘ operator redirects the standard error (file descriptor 2) to ‘/dev/null‘, which discards the error - The ‘2¿‘ operator redirects the standard error (file descriptor 2) to ‘/dev/null‘, which discards the error
messages. messages.

2. Running a Program in the Background 2. Running a Program in the Background


1 ./ prog . exe & 1 ./ prog . exe &

- The ‘‘ operator runs the command in the background. - The ‘‘ operator runs the command in the background.
Function Prologue 3. Changing File Permissions
1 _main : 1 chmod 751 datafile
2 LFB1 :
3 pushl % ebp - ‘chmod 751‘ sets the permissions to ‘-rwxr-x–x‘ (owner can read, write, and execute; group can read and
4 LCFI0 : execute; others can execute).
5 movl % esp , % ebp
6 LCFI1 :
7 subl $16 , % esp 4. Appending Output to a File While Displaying in Terminal
8 call ___x86.get_pc_thunk.ax
1 prog . exe | tee -a out . txt
- These instructions set up the stack frame for the function. - pushl %ebp saves the base pointer of
the calling function. - movl %esp, %ebp sets the base pointer to the current stack pointer. - subl $16, - The ‘tee‘ command reads from standard input and writes to standard output and files. The ‘-a‘ option
%esp allocates 16 bytes of space on the stack for local variables. appends to the specified file.

Initializing Variables 5. Creating a Static Library


1 L1$pb : 1 ar rcs libveli . a / home / veli / PROJ /*. o
2 movl $3 , -4(% ebp )
- ‘ar rcs‘ creates an archive (static library) named ‘libveli.a‘ from the object files in the specified directory.
3 movl $4 , -8(% ebp )
4 movl $5 , -12(% ebp )

- movl $3, -4(%ebp) sets x to 3. - movl $4, -8(%ebp) sets y to 4. - movl $5, -12(%ebp) sets z to 6. Remote Login with Secure Shell
5.
1 ssh veli@mach1 . cmpe . boun . edu . tr

- ‘ssh‘ is used to log into a remote machine securely. The syntax is ‘ssh username@hostname‘.
First Addition Operation
1 movl -4(% ebp ) , % edx 7. Displaying the Manual Page
2 movl -8(% ebp ) , % eax
3 addl % eax , % edx 1 man gcc
4 movl -12(% ebp ) , % eax
5 addl % edx , % eax - ‘man‘ displays the manual page for the specified command.
6 movl % eax , -4(% ebp )

- Loads x into edx. - Loads y into eax. - Adds y to x (stored in edx). - Loads z into eax. - Adds z to 8. Listing All Files Including Hidden Ones
the result (stored in edx), making it x = x + y + z. - Stores the result back into x.
1 ls -a

Second Addition Operation - The ‘-a‘ option for ‘ls‘ lists all files, including hidden ones (those starting with a dot).

1 movl -4(% ebp ) , % edx


2 movl -12(% ebp ) , % eax 9. Killing a Process by ID
3 addl % edx , % eax
4 movl % eax , -8(% ebp ) 1 kill -9 321

- Loads x into edx. - Loads z into eax. - Adds x to z. - Stores the result back into y, making it y = - ‘kill -9‘ forcefully terminates the process with the specified process ID (321).
x + z. This lecture provides a detailed understanding of the Linux commands required for Problem 1. Practice
these commands to become more familiar with their usage.
Third Addition Operation
1 movl -4(% ebp ) , % edx
2 movl -8(% ebp ) , % eax
3 addl % edx , % eax
4 movl % eax , -12(% ebp )

- Loads x into edx. - Loads y into eax. - Adds x to y. - Stores the result back into z, making it z =
x + y.

2 2

Function Epilogue 2022 - Problem 5 (20 pts)


1 movl $0 , % eax The following assembly code was produced by the GNU C compiler by compiling a C program with the
2 leave gcc -S -m32 command. The C program only had a main function with some variable definitions and
3 LCFI2 :
4 ret
statements. Only a part of the generated assembly code is shown below:
1 _main :
- movl $0, %eax sets the return value to 0. - leave restores the base pointer and stack pointer. - ret 2 LFB1 :
returns from the function. 3 pushl % ebp
This detailed analysis helps in understanding how the assembly instructions correspond to the C code 4 LCFI0 :
5 movl % esp , % ebp
and the overall flow of the program.
6 LCFI1 :
7 subl $16 , % esp
8 call ___x86.get_pc_thunk.ax
9 L1$pb :
10 movl $3 , -4(% ebp )
11 movl $4 , -8(% ebp )
12 movl $5 , -12(% ebp )
13 movl -4(% ebp ) , % edx
14 movl -8(% ebp ) , % eax
15 addl % eax , % edx
16 movl -12(% ebp ) , % eax
17 addl % edx , % eax
18 movl % eax , -4(% ebp )
19 movl -4(% ebp ) , % edx
20 movl -12(% ebp ) , % eax
21 addl % edx , % eax
22 movl % eax , -8(% ebp )
23 movl -4(% ebp ) , % edx
24 movl -8(% ebp ) , % eax
25 addl % edx , % eax
26 movl % eax , -12(% ebp )
27 movl $0 , % eax
28 leave
29 LCFI2 :
30 ret

Give the full C code that corresponds to the above GNU assembly code (in other words, disassemble
the GNU assembly code given above).

Answer
Here is the full C code corresponding to the given assembly code:
1 # include < stdio .h >
2
3 int main () {
4 int x , y , z ;
5 x = 3;
6 y = 4;
7 z = 5;
8 x = x + y + z;
9 y = x + z;
10 z = x + y;
11 return (0) ;
12 }

Lecture: Understanding Problem 5


In this lecture, we will analyze the given assembly code to understand how it maps to the corresponding
C code.
Detailed Analysis of Each Statement 2022 - Problem 3 (20 pts)
1. **Statement (a): ‘az = ax + ay + az;‘** - ax and ay are private and protected members of class A, Consider the following C++ program. Determine whether each statement (a-j) will compile successfully
respectively. - setaz is a public member function of class A and can access both ax and ay. - **Result:** or result in a compiler error.
OK
1 # include < iostream >
2. **Statement (b): ‘bx = u + 4 * ax;‘** - ax is a private member of class A and is not accessible in
2 using namespace std ;
class B. - **Result:** ERROR 3
3. **Statement (c): ‘dy = u + 3 * cy;‘** - cy is a protected member of class C and is accessible in 4 class A {
class D, which is derived from C. - **Result:** OK 5 private :
6 int ax ;
4. **Statement (d): ‘dx = 2 * u;‘** - dx is a private member of class D and can be accessed within its
7
member function setdx1. - **Result:** OK 8 protected :
5. **Statement (e): ‘dx = u + 3 * bx;‘** - bx is a private member of class B and is not accessible in 9 int ay ;
class D. - **Result:** ERROR 10

6. **Statement (f): ‘myDobj.setdx1(4);‘** - setdx1 is a public member function of class D and can be 11 public :
12 int az ;
called on an object of class D. - **Result:** OK
13
7. **Statement (g): ‘myDobj.setdx2(4);‘** - setdx2 is a public member function of class D and can be 14 void setaz ( int u ) {
called on an object of class D. - setdx2 accesses dx, a private member of D. - **Result:** OK 15 az = ax + ay + az ; /* ( a ) */
8. **Statement (h): ‘myDobj.setdy(4);‘** - setdy is a protected member function of class D and cannot 16 }
be called on an object of class D from outside the class. - **Result:** ERROR 17 };
18
9. **Statement (i): ‘cout ¡¡ myDobj.dz ¡¡ endl;‘** - dz is a public member of class D and can be accessed
19 class B : public A {
directly. - **Result:** OK 20 private :
10. **Statement (j): ‘myDobj.dy = 20;‘** - dy is a protected member of class D and cannot be accessed 21 int bx ;
directly from outside the class. - **Result:** ERROR 22

This detailed analysis helps in understanding the access control mechanisms in C++ and the resulting 23 protected :
24 int by ;
compilation errors or successes.
25
26 public :
27 int bz ;
28
29 void setby ( int u ) {
30 bx = u + 4 * ax ; /* ( b ) */
31 }
32 };
33
34 class C : public A {
35 protected :
36 int cy ;
37
38 public :
39 int cz ;
40 };
41

42 class D : public C {
43 private :
44 int dx ;
45
46 protected :
47 int dy ;
48 void setdy ( int u ) {
49 dy = u + 3 * cy ; /* ( c ) */
50 }
51
52 public :
53 int dz ;
54
55 void setdx1 ( int u ) {
56 dx = 2 * u ; /* ( d ) */
57 }
58
59 void setdx2 ( int u ) {
60 dx = u + 3 * bx ; /* ( e ) */

3 1

2022 - Problem 2 (20 pts) 61 }


62 };
63
Consider the following program prog.c. It is compiled with the command: g++ prog.c (i.e., for a 64-bit
64 int main () {
system). What is the output of the program?
65 D myDobj ;
1 # include < iostream > 66 C myCobj ;
2 using namespace std ; 67
3 68 myDobj . setdx1 (4) ; /* ( f ) */
4 template < class T > 69 myDobj . setdx2 (4) ; /* ( g ) */
5 T func1 ( T x , T y ) { 70 myDobj . setdy (4) ; /* ( h ) */
6 return ( x + y ) ; 71 cout << myDobj . dz << endl ; /* ( i ) */
7 } 72 myDobj . dy = 20; /* ( j ) */
8 73
9 class A { 74 return 0;
10 private : 75 }
11 int x ;
12
Write OK if the statements (a-j) will compile successfully or write ERROR if the compiler will report an
13 public : error.
14 A ( int i ) {
15 x = i;
16 cout << x << endl ;
17 }
Answer
18 ~ A () {
(a) OK
19 x = x + x;
20 cout << x << endl ;
21 }
(b) ERROR
22 void g () {
23 x = func1 < int >( x , 1) ;
(c) OK
24 }
25 int getx () { (d) OK
26 return ( x ) ;
27 } (e) ERROR
28 };
29
(f) OK
30 void func2 ( A * & pobj ) {
31 A fobj (1) ; (g) OK
32

33 pobj = new A (2) ; (h) ERROR


34 pobj = new A (3) ;
35 pobj - > g () ; (i) OK
36
37 fobj = * pobj ; (j) ERROR
38 }
39
40 int main () { Lecture: Understanding Problem 3
41 A obj (4) ;
42 A * pobj ; In this lecture, we will analyze the given C++ program to understand why certain statements will compile
43
44 cout << obj . getx () << endl ; successfully while others will result in compiler errors.
45 func2 ( pobj ) ;
46 obj . g () ;
47 cout << sizeof ( pobj ) << endl ;
Access Control in C++
48 cout << obj . getx () << endl ; C++ provides three access specifiers: private, protected, and public.
49
50 delete ( pobj ) ;
51 return 0; • private members are accessible only within the same class.
52 }
• protected members are accessible within the same class and by derived classes.
Write your answer here:
• public members are accessible from any part of the program.

Answer
The program output is as follows:
1 4
2 4
Problem 3 (20 pts) 3 1
Consider the following C++ program: 4 2
5 3
#include <iostream> class D : public C {
6 8
using namespace std; private:
int dx ; 7 8
class A { 8 5
private: protected: 9 8
int ax ; int dy ;
void setdy(int u) { 10 10
protected: dy = u + 3*cy ; /* (c) */
int ay ; }

public: public:
int az ; int dz ; Lecture: Understanding Problem 2
void setaz(int u) { void setdx1(int u) {
az = ax+ay+az ; /* (a) */ dx = 2*u ; /* (d) */ In this lecture, we will analyze the given C++ program to understand its output when compiled and
} } executed.
};
void setdx2(int u) {
class B : public A { dx = u + 3*bx ; /* (e) */
private: } Object Construction and Destruction
int bx ; };

protected: 1. **Creating object ‘obj‘ with value 4:** - Calls the constructor A(int i) with i = 4. - Sets x to 4 and
int by ; int main() { prints 4. - Destructor is defined but not called yet.
public: D myDobj ; 2. **Printing x of obj:** - Calls obj.getx() which returns 4. - Prints 4.
int bz ; C myCobj ;

void setby(int u) { myDobj.setdx1(4) ; /* (f) */


bx = u + 4*ax ; /* (b) */ myDobj.setdx2(4) ; /* (g) */ Function func2
} myDobj.setdy(4) ; /* (h) */
}; cout << myDobj.dz << endl ; /* (i) */ 3. **Function func2(pobj):** - Creates local object fobj with value 1. - Calls constructor with i = 1,
myDobj.dy = 20 ; /* (j) */
class C : public A { sets x to 1, and prints 1. - Assigns pobj to new object with value 2. - Calls constructor with i = 2, sets
protected: return(0) ; x to 2, and prints 2. - Reassigns pobj to new object with value 3. - Calls constructor with i = 3, sets
int cy ;
} x to 3, and prints 3. - Calls pobj->g(): - Increments x by 1 (using func1<int>(x, 1)), x becomes 4. -
public: Assigns fobj to *pobj: - Calls destructor for fobj. - Doubles x from 1 to 2 and prints 2. - Destructor for
int cz ;
}; the temporary ‘A(3)‘ object prints 8 (double of 4).

Main Function Continuation


4. **Calls obj.g():** - Increments x by 1 (using func1<int>(x, 1)), x becomes 5.
Write OK if the statements (a-j) will compile successfully or write ERROR if the compiler will
5. **Prints size of pobj:** - Prints 8 (pointer size on a 64-bit system).
report an error.
(a) (b) (c) (d) (e) (f) (g) (h) (i) (j)
6. **Prints x of obj:** - Calls obj.getx() which returns 5. - Prints 5.
7. **Deletes pobj and calls its destructor:** - Destructor ‘ A()‘ is called for ‘pobj‘. - ‘x‘ is doubled
from ‘4‘ to ‘8‘ and ‘8‘ is printed.
This detailed analysis helps in understanding the sequence of constructor and destructor calls, and the
changes in variable values leading to the final output.
Problem 4 (24 pts)
Write a QT program that will display and do the following:
• A line-edit widget for inputting a word,
• An LCD display object that shows the number of characters in the word that is entered
in the line-edit object,
• A capitalize button which capitalizes the word in the line-edit object,
• A close button that quits the program.

Note that a QT string object can be capitalized using toUpper method.

Problem 5 (20 pts)


The following assembly code was produced by the GNU C compiler by compiling a C program
with the gcc –S –m32 command . The C program only had a main function with some
variable definitions and statements. Only a part of the generated assembly code is shown
below:

Name: Student No:


Generated GNU assembly code C Code (write your answer here) 1 2 3 4 5
_main:
LFB1: A
pushl %ebp
LCFI0:
movl %esp, %ebp CMPE 230: Systems Programming, Spring 2022, Final
LCFI1:
subl $16, %esp Problem 1 (16 pts)
call ___x86.get_pc_thunk.ax
L1$pb:
Give the Linux commands that will do the following:
movl $3, -4(%ebp) (a) List symbols from the static library libgeom.a .
movl $4, -8(%ebp)
movl $5, -12(%ebp) (b) Compile the C file func.c in order to produce a positional independent object file that can be
movl -4(%ebp), %edx
movl -8(%ebp), %eax
added to a shared library.
addl %eax, %edx
movl -12(%ebp), %eax (c) Add the current directory to the path environment variable.
addl %edx, %eax
movl %eax, -4(%ebp)
movl -4(%ebp), %edx
(d) Change the current directory two levels up .
movl -12(%ebp), %eax
addl %edx, %eax (e) Copy all the files in the current directory starting with “cmpe230” and ending with “.txt” to a
movl %eax, -8(%ebp) sub-directory named “MYDIR”.
movl -4(%ebp), %edx
movl -8(%ebp), %eax
addl %edx, %eax (f) Print the contents of a directory in long format.
movl %eax, -12(%ebp)
movl $0, %eax (g) Display the contents of a file named “file.txt” page by page.
leave
LCFI2:
ret (h) Switch current user to john.

Give the full C code that corresponds to the above GNU assembly code. (in other words,
disassemble the GNU assembly code given above).
Problem 2 (20 pts)
Consider the following program prog.c. It is compiled with the command:
g++ prog.c (i.e. for 64-bit system)
What is the output of the program ?

#include <iostream> void func2(A * & pobj ) { Write your answer


using namespace std; A fobj(1) ; Here:

template <class T> pobj = new A(2) ;


T func1(T x, T y) { pobj = new A(3) ;
return(x+y) ; pobj->g() ;
}
fobj = *pobj ;
class A { }
private:
int x ; int main() {
A obj(4) ;
public: A *pobj ;
A(int i) {
x = i ; cout << obj.getx() << endl ;
cout << x << endl ; func2(pobj) ;
} obj.g() ;
~A() { cout << sizeof(pobj) << endl ;
x = x + x ; cout << obj.getx() << endl ;
cout << x << endl ;
} delete(pobj) ;
void g() { return 0;
x = func1<int>(x,1) ; }
}
int getx() {
return(x) ;
}
};
QT EXAMPLES
Problem 1 Problem 4
BUTTON
a) nm libgeom.a
b) gcc –fpic func.c Code written by each student
c) export PATH=./$PATH
d) cd ../..
e) cp cmpe230*.txt MYDIR/
f) ls –l
g) more file.txt
h) su john

Problem 2 Problem 5

4 #include <stdio.h>
4
1 int main()
{
2
int x, y, z ;
3
8 x = 3 ;
8 y = 4 ;
5 z = 5 ;
8
10 x = x + y + z ;
y = x + z ;
z = x + y ;
Problem 3
return(0) ;
}
CURRENCY
a) ok
b) error
PRO: MAİN: c) ok
d) ok
e) error
f) ok
g) ok
h) error
i) ok
j) error

QT EXAMPLES QT EXAMPLES
HELLO

MYCLASS.H:

SPINSLIDER
MYCLASS.CPP:
QT EXAMPLES QT EXAMPLES
FINDDIALOG

MAIN.CPP: DESİGNER

FORM.UI:

FINDDIALOG.H:

MAIN.CPP: FORM.H:

QT EXAMPLES QT EXAMPLES
FINDDIALOG.CPP:

FORM.CPP:

BUTTONGROUP

PRO:
QT EXAMPLES
LINGUIST

LINGUIST.CPP: PRO:

You might also like