QUESTION IMAGE
Question
use the code that was given in the video to perform the following tasks:
create two dword variables. prompt for input and store the values input from the keyboard into the variables. subtract the second number from the first and output the difference. using logic, swap the values in the two variables for each other. output the swapped values.
here is an example of how your program should run:
input a number
18
input a number
5
18 - 5 = 13
after swapping
first variable: 5
second variable: 18
please note:
you are not to use bit wise operations like xor, and, and or to swap variables around. you are also not to use addition and/or subtraction to swap the variables around. you should simply use the mov instruction and general purpose registers.
not putting comments in assembly language source code is unacceptable. you are required to comment each line of code you write. failure to do so will result in a 20% reduction in your grade.
To solve this problem, we will write an x86 assembly language program that creates two DWORD variables, takes user input, performs subtraction, swaps the values of the variables, and outputs the results. We will use the mov instruction and general-purpose registers for swapping, as specified.
Approach
- Define Variables: Declare two DWORD variables to store the input values.
- Prompt for Input: Use system calls to prompt the user for two numbers and store them in the variables.
- Subtract and Output: Subtract the second number from the first and display the result.
- Swap Values: Use a general-purpose register to temporarily hold one value while swapping the values of the two variables.
- Output Swapped Values: Display the values of the variables after swapping.
Solution Code
.MODEL FLAT, C
.STACK 4096
.DATA
prompt1 DB 'Input a number', 0ah, 0dh, 0 ; Prompt for first number
prompt2 DB 'Input a number', 0ah, 0dh, 0 ; Prompt for second number
resultMsg DB '%d - %d = %d', 0ah, 0dh, 0 ; Message for subtraction result
swapMsg1 DB 'After Swapping', 0ah, 0dh, 0 ; Message before swapped values
swapMsg2 DB 'first variable: %d', 0ah, 0dh, 0 ; Message for first swapped variable
swapMsg3 DB 'second variable: %d', 0ah, 0dh, 0 ; Message for second swapped variable
num1 DWORD ? ; First DWORD variable
num2 DWORD ? ; Second DWORD variable
.CODE
EXTRN _printf:PROC, _scanf:PROC ; External functions for input/output
main PROC
; Prompt for first number
push OFFSET prompt1 ; Push address of prompt1
call _printf ; Call printf to display prompt1
add esp, 4 ; Clean up stack
; Read first number into num1
push OFFSET num1 ; Push address of num1
push OFFSET prompt1 ; Push format string (same as prompt1 for simplicity, but should be "%d")
call _scanf ; Call scanf to read input
add esp, 8 ; Clean up stack
; Prompt for second number
push OFFSET prompt2 ; Push address of prompt2
call _printf ; Call printf to display prompt2
add esp, 4 ; Clean up stack
; Read second number into num2
push OFFSET num2 ; Push address of num2
push OFFSET prompt2 ; Push format string (same as prompt2 for simplicity, but should be "%d")
call _scanf ; Call scanf to read input
add esp, 8 ; Clean up stack
; Subtract num2 from num1 and display result
mov eax, num1 ; Load num1 into EAX
sub eax, num2 ; Subtract num2 from EAX (num1 - num2)
push eax ; Push result of subtraction
push num2 ; Push num2
push num1 ; Push num1
push OFFSET resultMsg ; Push address of resultMsg
call _printf ; Call printf to display result
add esp, 16 ; Clean up stack (4 parameters * 4 bytes)
; Swap num1 and num2 using a temporary register (EBX)
mov eax, num1 ; Load num1 into EAX
mov ebx, num2…- Variable Declaration: We declare two DWORD variables
num1andnum2to store the input values. We also define strings for prompts and output messages. - Input Handling: We use
_printfto display prompts and_scanfto read user input into the variables. The stack is cleaned up after each function call. - Subtraction: We load
num1into theEAXregister, subtractnum2from it, and then use_printfto display the result. - Swapping Values: We use the
EAXandEBXregisters to temporarily hold the values ofnum1andnum2. We then move the value ofnum2(stored inEBX) intonum1and the value ofnum1(stored inEAX) intonum2. - Output After Swapping: We display the "After Swapping" message followed by the values of the swapped variables using
_printf.
This approach ensures that we follow the requirements of using the mov instruction and general-purpose registers for swapping, and we properly comment each line of code for clarity.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
- Variable Declaration: We declare two DWORD variables
num1andnum2to store the input values. We also define strings for prompts and output messages. - Input Handling: We use
_printfto display prompts and_scanfto read user input into the variables. The stack is cleaned up after each function call. - Subtraction: We load
num1into theEAXregister, subtractnum2from it, and then use_printfto display the result. - Swapping Values: We use the
EAXandEBXregisters to temporarily hold the values ofnum1andnum2. We then move the value ofnum2(stored inEBX) intonum1and the value ofnum1(stored inEAX) intonum2. - Output After Swapping: We display the "After Swapping" message followed by the values of the swapped variables using
_printf.
This approach ensures that we follow the requirements of using the mov instruction and general-purpose registers for swapping, and we properly comment each line of code for clarity.