-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2.asm
More file actions
91 lines (81 loc) · 1.75 KB
/
Copy pathp2.asm
File metadata and controls
91 lines (81 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
BITS 32
SECTION .data ;Data segment
progMsg DB 'The Multiplying Program',0xa ;Explain a program
lenProgMsg EQU $-progMsg ;The length of the program message
userMsg DB 'Please enter a single digit number: ' ;Ask the user to enter a number
lenUserMsg EQU $-userMsg
answerMsg DB 'The answer is: ' ;Show the answer
lenAnswerMsg EQU $-answerMsg
newLineMsg DB '',0xa
lenNewLineMsg EQU $-newLineMsg
SECTION .bss ;Uninitialized data
num1 RESB 5
num2 RESB 5
answer RESB 5
SECTION .text ;Code Segment
GLOBAL _start
_start: ;Program prompt
mov eax, 4 ;SYS_WRITE
mov ebx, 1 ;STDOUT
mov ecx, progMsg
mov edx, lenProgMsg
int 80h
;for a first digit number
;User prompt
mov eax, 4 ;SYS_WRITE
mov ebx, 1 ;STDOUT
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
;Read and store the user input
mov eax, 3 ;SYS_READ
mov ebx, 0 ;STDIN
mov ecx, num1
mov edx, 2
int 80h
;for the second digit number
;User prompt
mov eax, 4 ;SYS_WRITE
mov ebx, 1 ;STDOUT
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
;Read and store the user input
mov eax, 3 ;SYS_READ
mov ebx, 0 ;STDIN
mov ecx, num2
mov edx, 2
int 80h
mov ax,0 ;zero out ax.
;convert a string to a number
;the first number
mov al, [num1]
sub al, '0'
mov ah, [num2]
sub ah, '0'
;multiplication
imul ah
add al, '0' ;convert the answer from a number to a string
mov [answer], al
;Output the message
mov eax, 4 ;SYS_WRITE
mov ebx, 1 ;STDOUT
mov ecx, answerMsg
mov edx, lenAnswerMsg
int 80h
;Output the number entered
mov eax, 4 ;SYS_WRITE
mov ebx, 1 ;STDOUT
mov ecx, answer
mov edx, 5
int 80h
;Output the message
mov eax, 4 ;SYS_WRITE
mov ebx, 1 ;STDOUT
mov ecx, newLineMsg
mov edx, lenNewLineMsg
int 80h
;Exit code
mov eax, 1 ;SYS_EXIT
mov ebx, 0
int 80h