'Load only first byte into a register, from a string at a label

I am trying to load only the first byte in a register after data is passed into it for example if I have the following ASM code

global _start
section .data
string db "Hello", 0x00 ; string = Hello
section .text
_start:
mov eax, string ; move "Hello" into eax (just to store it for now)

this is where I am stuck, I want to ONLY store "H" into eax, so for instance if I moved "H" into EAX, then moved eax into edx so EDX = "H", I could do:

mov ebx, 1
mov ecx, edx ; This is so I can store "H" in ecx
mov edx, 12 ; 12 is longer than the string, however, I am just using this to test, as it should only output "H", rather than anything else, the reason for this is later I want to use "H" in a  cmp statement
int 0x80 ; execute the system call

Now I have also tried using al, ex: mov [edx], al after moving string into eax, but this still outputs the full string, if a working example can be provided for this, that would be greatly appreciated



Solution 1:[1]

To move the first byte of the string into al (which is the lowest byte of eax), use

mov al, [string]

If you want to move the byte into the low byte of eax and zero out the rest of eax, you can do

movzx eax, byte [string]

But you don't have to use EAX for that. You can load the byte into whatever register you want. Specifically EAX is probably a bad choice, since int 80h expects the syscall number there. It's not immediately obvious from the pasted code what are you trying to do with that "H" character afterwards...


If you are going to print the string to the console using the write syscall (#4) with the stdout file descriptor (1), which sounds like the natural thing to do, then, like fuz said, you don't need to load the contents of the string into any registers; the syscall expects the address of the string rather than its contents. To the best of my knowledge, there's no single character output in Linux.


In general, you seem to be confused between the address of the string and the contents on the string.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1