[SOLVED] Question assigning numbers to registers for nasm
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How do I assign the number 0x31 to the eax register. the code below doesn't seem to do the job for sys_write. Is it possable to do that without defining
a variable?
; Print the number of arguments
mov edx,100
pop ecx ; Get the number of arguments
mov byte [sbyte], cl
You are treating the number of arguments as an ascii code. But it isn't. You need to convert it to ascii. If you know it will be just one digit, then converting to ascii is just adding 30h to it. If it might be more than one digit, converting it to ascii requires first breaking it up into decimal digits.
Also, why do you set edx to 100? Aren't you trying to print just one digit?
Also, why do you think you get the number of arguments with that pop instruction? I'm not sure what is where when asm code starts in Linux, but I didn't expect the stack layout you seem to expect.
why doesn't this print a number and how can I make it print the number 1?
Code:
; Print the number 1
mov eax,1 ;system call number (sys_write)
mov byte [sbyte], 31h
mov ecx, [sbyte]
mov edx,10 ;Amount of data to right
int 80h
In nasm syntax the characters I marked in red [ ] tell it you want the contents of the memory location. But you need ecx to hold the address, not the contents. So try it without the [ ].
But on the line above that, the [ ] are correct, because you want the 31h to be stored as the contents of that location.
One minor point, just to satisfy my inner pedant, and because I think if you're going to be an assembler programmer you might as well talk like one.
In assembler, we generally don't use the term 'assign' in this context. It is not the same as assigning a value to a variable in a high level language. You are specifying/writing a specific machine instruction, and that instruction is to load an immediate value into a specified register. The immediate data is part of the code and will appear as the byte(s) immediately following the opcode byte(s) in the binary object code.
I know it is a bit of a nit, but nits left unpicked can grow into full-on bugs.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.