How can I get access to a memory address directly with asm?

Sup! I've been working on sixlang and got an idea - to make increasing / decreasing operators (like in C). Rust's += 1 generates add 1, not inc. I tried to do this via core::arch::asm but it passes arguments through registers so i did only this. Does someone know how can i pass an address without in(reg)? :sweat_smile:

That code is correct except for that &A needs to be &mut A to avoid UB. The generated assembly looks correct.

Mutability is not the case. Take a look at generated assembly code

	push	rax
	lea	rax, [rip + _ZN10playground4main1A17h10aff76bb05bbbb6E]
	inc	byte ptr [rax]
	pop	rax

	.zero	1

I can only get access to A via rax or other registers if i use inout(reg). I need

	inc [_ZN10playground4main1A17h10aff76bb05bbbb6E]

	.zero	1

You don't need inout as the reference itself doesn't get mutated. Also

lea rax, [rip + _ZN10playground4main1A17h10aff76bb05bbbb6E]
inc byte ptr [rax]


inc byte ptr [rip + _ZN10playground4main1A17h10aff76bb05bbbb6E]

do exactly the same. The only difference is that the first splits the address computation out, while the second doesn't.

I meant in or out or inout but it’s not the case. Passing arguments via register doesn’t give us additional performance this time and makes our binary bigger. I just need direct access to label, so don’t try to bring me over please. If you know how to do what I need directly, please tell me how to, otherwise thank you for wasting time on me, but your advices, I think, will be useless :pensive:

If you can use nightly, this will work: Rust Playground If not, I don't think there is anything else you can do.

Thank you! That is it :grin:

There is only one problem: i get linker error while trying to run your code. Looks like statics are allocated in .rodata and mutable statics in .bss, not in .data :frowning:

UPD: I applied #[link_section = ".data"] but it wasn't a solution, i still get same linker error.

relocation R_X86_64_32S against `.bss._ZN10playground4main1A17hf2a75aa4b04c1505E' can not be used when making a PIE object; recompile with -fPIE
relocation R_X86_64_32S against `.data' can not be used when making a PIE object; recompile with -fPIE

Right, forgot the rip + as necessary for position independent code: Rust Playground

Only zero initialized statics. .bss isn't stored on the disk, but initialized with zeros by the dynamic linker. This saves space. Other than that there is no difference between .data and .bss.

Yeah, thanks, i understood!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.