Extra
Extra
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.
[bp + di], al
inc si
al, [bp + si - 1]
jnz loop1
!
!
!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) */
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
1 _start :
2 mov dword [ a ] , 1 ; a = 1
3 mov dword [ i ] , 1 ; i = 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.
- 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
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
(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.
- 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
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
10 public :
1 1
- 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
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).
- The ‘tee‘ command reads from standard input and writes to standard output and files. The ‘-a‘ option
appends to the specified file.
- ‘ar rcs‘ creates an archive (static library) named ‘libveli.a‘ from the object files in the specified directory.
- ‘ssh‘ is used to log into a remote machine securely. The syntax is ‘ssh username@hostname‘.
- 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
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
8. ls -a 8. ls -a
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.
- 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.
- 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).
- 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
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 }
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
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 ;
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 ?
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: