Last active
May 7, 2024 01:45
-
-
Save kenedii/3f0575eeec113bafe22c84935fc687a3 to your computer and use it in GitHub Desktop.
takes user input for a number and squares it. Written in MASM32 x86 assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include \masm32\include\masm32rt.inc | |
.data | |
prompt db "input a number to square here: ", 0 | |
prntNumBuffer dw ? | |
prntSqrBuffer dw ? | |
prompt2 db "your number was: ", 0 | |
prompt3 db "the square is: ", 0 | |
square dw ? | |
success1 db "Successfully squared your integer. ",13,10,0 | |
integer dw ? | |
newline db 13,10,0 | |
dummy db " ",13,10,0 ; dummy variable to print a new line | |
.code | |
start: | |
invoke StdOut, offset prompt ; prints the prompt | |
invoke crt_scanf, chr$("%d"), addr integer | |
invoke StdOut, offset prompt2 ; prints the prompt2 | |
movzx eax, integer ; move number to convert to string into eax | |
lea edi, prntNumBuffer ; buffer to store converted number in | |
call to_string ; convert number to string before printing | |
invoke StdOut, offset prntNumBuffer ; print the inputted number | |
invoke StdOut, offset dummy ; print a new line | |
push offset integer | |
call square_number ; square the number using square_number proc= | |
movzx eax, square | |
lea edi, prntSqrBuffer | |
call to_string ; convert the squared input to string | |
invoke StdOut, offset prompt3 ; print the prompt3 | |
invoke StdOut, offset prntSqrBuffer ; print the square | |
invoke StdOut, offset dummy ; print a new line | |
invoke StdOut, offset success1 | |
square_number PROC | |
mov ax, integer | |
mov cx, integer | |
mul cx ; multiply ax=ax*cx where cx=ax=integer | |
mov square, ax ; move the result from ax->square | |
ret | |
square_number ENDP | |
to_string PROC ; converts a decimal number to a string | |
mov ebx, 10 | |
xor ecx, ecx | |
repeated_division: | |
xor edx, edx | |
div ebx | |
push dx | |
add cl,1 | |
or eax,eax | |
jnz repeated_division | |
load_digits: | |
pop ax | |
or al, 00110000b ; transforms to ascii | |
stosb ; store al into edi. edi = pointer to buffer | |
loop load_digits | |
mov byte ptr [edi], 0 | |
ret | |
to_string ENDP | |
exit | |
end start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment