Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assemblyでloopを回したい。 #1

Open
lemolatoon opened this issue Dec 12, 2021 · 0 comments
Open

Assemblyでloopを回したい。 #1

lemolatoon opened this issue Dec 12, 2021 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@lemolatoon
Copy link
Owner

loopが回らない

print_b.s内で

# print_b.s
.global print

print:
    mov $0, %rcx
    jmp loop

loop:
    (略)
    inc %rcx
    cmp $0x10, %rcx
    je loop
    ret

としていて、loopが16回回るはずなのに、一回しか回らない。

コンソール
$ gcc -nostdlib substart.s print_b.s hello.c -o hello
$ ./hello
実行結果
aaa
H
Hello World
# substart.s
.global _start

_start:
    call main
        mov $60, %eax           # exit
        xor %rdi, %rdi          # mov $0, %rdi
        syscall
// hello.c
void print(const void* c);

int main(void) {
        char str[] = "Hello World";
        print(&"a");
        print(&"a");
        print(&"a");
        print(&"\n");
        print(&str);
        print(&"\n");
        for (int i = 0; i < sizeof(str) / sizeof(char); i++) {
                print(&str[i]);
        }
        print(&"\n");
        return 0;
}

push, popできない

syscall前後でrcxが破壊されていると考えてpush, popしようとして、コメントアウトを外した。でも、gccによると

print_b.s: Assembler messages:
print_b.s:13: Error: invalid instruction suffix for `push'
print_b.s:15: Error: invalid instruction suffix for `pop'

と言われ、アセンブルすらできない。

@lemolatoon lemolatoon added the help wanted Extra attention is needed label Dec 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant