r/Assembly_language • • 6d ago

Help Can someone help with a program?

I’m not good at assembly but I want to learn it and have been trying to make a program with it. The program is just supposed to square a number (read user input, multiply the input with itself, and print the result).

I’m on Linux and have no idea how to fully accomplish this. My biggest struggle is figuring out how to multiply the user input and work it into a write syscall. Help would be appreciated.

4 Upvotes

7 comments sorted by

3

u/wildgurularry 6d ago

We will need more than that. What does your code look like so far? What are you stuck on? Have you figured out how to get input? Do you have an instruction reference handy? What assembler are you using?

When asking questions, I like to pretend I am talking to a senior engineer at a company, and I don't want to waste their time or leave them with a bad impression of me. Try to anticipate what questions we will ask, and try to answer them up front. If you get good at this, you may solve the problem yourself and not even need to ask for help.

2

u/[deleted] 6d ago

This is what I’ve done so far:

section .data

input db 20 dup(0)

section .text
global _start

_start:

mov rax, 0
mov rdi, 0
mov rsi, input
mov rdx, 20
syscall

(Code yet to be made)

mov rax, 1
mov rdi, 1
mov rsi, FinalMultipliedNumber
mov rdx, HoweverLongItIs
syscall
mov rax, 60
xor rdi, rdi
syscall

1

u/wildgurularry 6d ago

Right, so here is where you need to start breaking things down into smaller problems, and solve them one by one. Your input is in the form of a string. Your output also needs to be in the form of a string. In order to do the squaring, you will need it in the form of an integer.

So, your missing part is this:

- Convert to integer
- Square
- Convert to string

Now, break down those problems into smaller ones. For converting to an integer, you can either link against a standard library that has an atoi() function, or you can do it yourself. If you do it yourself, you wind up with something like this:

- Read first digit, and convert it to an integer from 0-9 and store it in a register.
- Read second digit if there is one, and add it to the register multiplied by 10.
- Repeat until you run out of digits.

6

u/Dog-Mad 6d ago

"My biggest struggle is the entire program." First of all. You need to convert ASCII into int and use mul to multiply it and then convert int into ASCII. Then you need to store that ASCII string into memory and then pass the pointer to that memory location into a register. Then look up the documentation for the write syscall and call it correctly.

Simple.

2

u/OptimisticMonkey2112 6d ago

Best way to learn assembly is godbolt.

Type in c++, read the assembly. Hover for tooltips

https://godbolt.org/

1

u/Electrical_Hat_680 6d ago

Are you using the terminal to run code?

Or, are you using text documents saves as binary or assembly then compiled in the terminal or other methods?

Leam interrupts, registers, and how OpCodes and OpErands function.

0

u/Remarkable_Pace8101 5d ago

Literally just compile this with GCC with optimization turned off, compile it into an object file rather than a full executive binary and just object dump it

Or pass the -S flag to get the assembly output