1

I have the following snippet which at the end of it I need to specify what is the value inside %rbx.

.global _start
.section .data
array: .long 0xf415, 0xf3561768, 0x8200f645
a: .short 1
b: .quad 0x670081b521c
.section .bss
.lcomm stam, 4
.section .text
_start:
xor %rcx, %rcx
xor %rax, %rax
xor %esi, %esi
lea array, %rbx

Since I am a total beginner in assembly, I want to try and break down the code line by line to test my understanding of it and I would love it if you could correct my mistakes.

.global _start states that our program starts to run from label _start.
.section .data states that we enter an initialization of "variables" section.
array: .long 0xf415, 0xf3561768, 0x8200f645 initialize an array of longs with 3 values 0xf415, 0xf3561768, 0x8200f645
a: .short 1 initializes variable a with one bit value of 1. b: .quad 0x670081b521c initializes variable b with 64 bit value of 0x670081b521c
.section .bss declaration of other variables without initialization
.lcomm stam, 4 declares variable stam and stores 4 bit for it.
.section .text declaring a read-only section.

_start:
xor %rcx, %rcx
xor %rax, %rax
xor %esi, %esi

starting the main program and initializes to 0 registers rcx, rax, esi.
lea array, %rbx - I am having a hard time understanding how is the effective address is calculated here since array has 3 values (if my assumptions were right)

EDIT:
Does lea array, %rbx "translate to" %rbx = 0xf415 + (0xf3561768 * 0x8200f645) ?

EDIT: If it is given that the start of the data sections in memory is 0x601038, does it mean that lea array, %rbx "translates to" %rbx = 0x601038?

Not sure it is relevant in this case but I am working on a processor that uses little-endian.

Eliran Turgeman
  • 1,526
  • 2
  • 16
  • 34
  • `array` is just the symbol / label address. Bytes you assemble into the output (e.g. with a `.long` pseudo-instruction) *after* that label can't affect its address. Also, never use LEA this way; either use `mov $array, %ebx` if you want 32-bit absolute addresses, or use `lea array(%rip), %rbx`. [How to load address of function or label into register in GNU Assembler](https://stackoverflow.com/q/57212012) – Peter Cordes Nov 07 '20 at 11:07
  • so the `array` symbol address is the start of the `.data` section, whatever that is. It's not an assemble-time constant; it would be set at link-time if linking into a non-PIE executable, otherwise not even then. – Peter Cordes Nov 07 '20 at 11:11
  • it's bad idea to strart assembler research with a protected mode task. run dosbox-x with masm – Алексей Неудачин Nov 07 '20 at 12:37
  • @PeterCordes regarding the LEA usage, I'll keep this in mind (it is not a code I have written it is a part of an hw assignment). Also, could you confirm my edit? did I understand it correctly? – Eliran Turgeman Nov 07 '20 at 13:55
  • 2
    @АлексейНеудачин : Strongly disagree with that suggestion. Real-mode is an extra level of complexity (segmentation), and non-orthogonal register addressing modes. 32-bit x86 is closer to the direction modern ISAs are evolving towards: a bunch of mostly non-special registers, farther from 8-bit micros. DOS itself is also basically useless, with different system calls than modern OSes. See also the beginner section at the top of https://stackoverflow.com/tags/x86/info, and my [Suggestions on how to learn asm, with a recommendation against 16bit DOS](https://stackoverflow.com/a/34918617) – Peter Cordes Nov 07 '20 at 14:01
  • but i didn't talk about real mode. you can do sector sized pm os to start with or make pm switch in dos exe . is it possible without dos/bios knowledge is another question . you should know bios at least – Алексей Неудачин Nov 07 '20 at 15:00
  • @PeterCordes assembler outside real mode and os development.... `Due to restricted access...` http://nicolascormier.com/documentation/sys-programming/os-programming/CPU_Rings_Privilege_and_Protection_Gustavo_Duarte.pdf that's it – Алексей Неудачин Nov 07 '20 at 15:21
  • 3
    Your understanding of `.global _start` is incorrect. This directive tells the assembler that `_start` is a global symbol. Nothing more, nothing less. It does not set the entry point of the program. – fuz Nov 07 '20 at 15:48
  • 1
    @АлексейНеудачин: Oh, right, you did say protected mode, not DOS just DOSBox. IMO learning how to do OS development for x86 (notoriously complex) at the same time you're learning asm at all is a big mistake. Anyone that understands C already understands how user-space works: OS gives you space for your code + data, you use it. Learning the basics of asm in that sandbox in a modern OS with a flat memory model makes it much easier to *then* learn about segment registers, GDT / IDT, and all that stuff if/when you ever choose to. (Not to mention the emulated legacy device IO ports.) – Peter Cordes Nov 07 '20 at 19:31

0 Answers0