att

X86 read from stdin and write to stdout without referring the standard library

廉价感情. 提交于 2020-12-30 03:55:33
问题 I'm a beginner in X86 assembly language. I know how to read from stdin and write to stdout using build-in functions, but I'm not sure how to do it with plain assembly code(i.e. manipulating registers and taking advantage of system calls). #include <stdio.h> #include <unistd.h> int main(){ /* copy input to output */ char buf[BUFSIZ]; int n; while ((n = read(0, buf, BUFSIZ)) > 0) write(1, buf, n); return 0; } This is the C code I wrote to first read from the standard input (represented by

x86-64 instruction set, AT&T syntax, confusion regarding lea and brackets

旧街凉风 提交于 2020-12-27 06:35:44
问题 I’ve been told that lea %rax, %rdx is invalid syntax as the source needs to be in brackets, i.e lea (%rax), %rdx I think I’ve clearly misunderstood both lea and the purpose of brackets. I thought that lea %rax, %rdx would move the memory address stored in %rax, to %rdx, but apparently this is what lea (%rax), %rdx does? What confuses me is that I thought brackets signify going to an address in memory, and taking the value at that address. So by using brackets lea would be moving a value from

What is the “-4” for in assembler: movl $1, -4(%rbp) [duplicate]

我怕爱的太早我们不能终老 提交于 2020-12-15 07:06:37
问题 This question already has answers here : What does the bracket in `movl (%eax), %eax` mean? (3 answers) What does a hexadecimal number, with a register in parenthesis mean in Assembly? (1 answer) Closed 22 days ago . int x=1; int y=2; int z=3; turns into movl $1, -4(%rbp) movl $2, -8(%rbp) movl $3, -12(%rbp) What is the -4,-8,-12 for ? Why is it going by 4's? 4 bytes = 32 bits? 回答1: -4 / -8 / -12 bytes relative to the address held in rbp , which is the pointer to the top of the stack (which

Calculating LCM in assembly x86

。_饼干妹妹 提交于 2020-12-12 05:35:11
问题 I have the following assembly code .global _start .section .text _start: movq a, %rax movq b, %rbx imul %rbx, %rax cmp %rbx, %rax je gcd_calculated ja l1 sub %rax, %rbx jmp _start l1: sub %rbx, %rax jmp _start gcd_calculated: div %rax movq %rax, (c) a,b are quads that I need to calculate their lcm and I need to assign the result to c I get wrong results with the above code and I can't spot why. generally, i'm relaying on the the lcm = (a*b)/gcd so I store a*b in %rax and then calculate the