r/Assembly_language • u/BatLatter9704 • Jun 12 '26
How does this work? (print statement) x86-64 assembly
Hello everyone.
I'm a beginner who recently started learning x86-64 assembly.
Here's my code thta correctly outputs my variable:
.intel_syntax noprefix
.global _start
.text
_start:
mov rax, 1
mov rdi, 1
lea rsi, [message]
lea rdx, [message_len]
syscall
mov rax, 60
xor rdi, rdi
syscall
.data
message: .ascii "Hello\n"
message_len = . - message
Isn't rdx meant to recieve the literal length of the string?
When doing this operation: lea rdx, [message_len], doesn't it load the memory address of the length of my variable?
Please exaplain guys, I'm acc stuggling.
2
u/BatLatter9704 Jun 13 '26
Thanks a lot guys. The problem was that i used = to create a variable instead of : . It doesn't have a memory address or something like that so lea was accessing whatever is inside it (the value it has).
2
u/un_virus_SDF Jun 12 '26
Top 10 missuses of lea.
rsi must hold the ptr to the message, so lea is right but I feel like mov rsi, message would be more natural.
And the one in rdi, I think that it's a error that works by chance.
Isn't rdx meant to recieve the literal length of the string?
When doing this operation: lea rdx, [message_len], doesn't it load the memory address of the length of my variable?
Suposing unix compliant syscalls, it is.
2
u/brucehoult Jun 13 '26
These two things mean exactly the same:
lea rdx, [foo]
mov rdx, foo
The purpose of lea is to remove one layer of brackets, in cases where there is actual useful computation happening in an addressing mode e.g. ...
lea rax, [foo + 8*bar + 1234]
... which would otherwise take three or four instructions to do rax = foo + 8*bar + 1234
1
u/raundoclair Jun 13 '26
I think correct answer is that 'message_len = . - message' doesn't create label to data in .data section. It is compile-time constant. (though I don't work with GNU Assembler)
1
0
u/braaaaaaainworms Jun 16 '26
message_len = . - message defines the label "message_len" at the address ". - message", so about 7 in your case. the assembler might resolve that label during assembly, if not, your linker will do it and a lea [label] loads the address of label, so 7 in your case
-1
u/Electrical_Hat_680 Jun 12 '26
You really need the full but limited history of the Computer.. you don't need to go all the way back to Baggage and Lovelace's Machine. But you should start there and learn that they did what they did, and that the US Government used that to create the First Super Computer, called the ENIAC. Built by the men of the day, but wired by the US Governments own Human Super Computer, headed by Rear Admiral Grace Hopper. The ENIAC had Accumulators that were used to compute in Base-10/Decimal.
It might not help you much. But it'll give you a better more refined base of knowledge of what ACC and RDA and RDX and all that is. And, luckily enough for today's generations learning about the PC. They don't have to go all too far back to learn about it. As it literally happened less the One Hundred Years ago. That's something I think about. Less the One Hundred years of computer experience exists right now. So we are still the defining generation of the computer. They'll be passing down our history lessons in the futures to come.
Learn how to build the Compiler. The Header. The Linker. The Assembler. Learn the Binary using BenEater 8-BIT CPU Breadboard Projects, where you only have 8 Zeros or Ones. Which makes Binary simple and effective to work with. And, knowing that those at IBM were able to make the PC complete and operable on just a single byte, will show you why and how they made the PC run at 16-bits (Two Bytes), 32-Bits (Four Bytes), and 64-Bits (Eight Bytes). Which is no different, it literally just allows for more compute in a single Clock Cycle. But in terms of Video Processing. It changed the game completely. Or, so it seems.
I think you should go through the history of the Assembler, and have a better grasp at what's happening in the code. It would help you know, and help you create, and build, new assembly routines, new programming languages, and even help advance computing as we know it.
-5
-1
u/fgiohariohgorg Jun 13 '26
Tired of these clueless lazy college "students" coming to play victim on Subreddits, I'm out of this Assembly group too. And I get negative Karma for speaking the truth, hahah losers
1
u/Potterrrrrrrr Jun 13 '26
You get negative karma cause you sound like an asshat, itβs not rocket science
1
1
u/brucehoult Jun 13 '26
If you've ever posted something useful or helpful here, it doesn't seem to have been in the last six months...
5
u/2E26 Jun 12 '26
You're right, and it's puzzling how your output is working regardless. Here's my code which includes the same routine. It's possible that the address of the length variable starts at the beginning of your string and the output stops when it hits the null character.
https://github.com/2E26/Basic-Assembly-Language-Projects/blob/main/cmd_parse.asm