인라인 어셈블리

jungwook의 이미지

안녕하세요?

유닉스, 리눅스 프로그래밍 필수 유틸리티를 공부하고 있는데,
예제를 실행할려는데 되지가 않네요.

코드는

#include <stdio.h>
 
int main()
{
  int a = 1;
  char *str = "a = %d, number = %d\n";
 
  __asm__ __volatile(
     "pushl %0\n\t"
     "pushl %1\n\t"
     "pushl %2\n\t"     
     "call  printf\n\t"
     :
     :
  );
 
  __asm__ __volatile("movl $4, %eax\n\t movl $5 %ebx\n");
  return 0;
}

이런 예제인데

컴파일이 되지 않네요.

메시지는
like1.c 8: Error: suffix or operands invalid for 'push'
이렇게 나오네요.

pushl을 push로 바꾸니까 컴파일은 되는데 세그먼트 폴트가 나고요.

답변 부탁드릴께요.

planetarium의 이미지

유닉스/리눅스 계열에서 쓰이는 AT&T 어셈블리와
윈도우에서 쓰이는 MASM의 문법이 약간 다릅니다.
제일 대표적인게 명령어 뒤의 l 등의 postfix 의 유무죠.
일단은 리눅스에서 다시 시도해보세요.

jungwook의 이미지

지금 리눅스에서 프로그램 작성한 것입니다.
제가 64비트 cpu인데 그것과 연관이 있는건 아닌가요?

shint의 이미지

어셈을 잘 몰라요 ㅋㅋ''
궁금해서 찾아보니 같은 질문이 있네요.

http://www.opensubscriber.com/message/linux-assembly@vger.kernel.org/6122292.html

답변을 보면 문법이 다르다정도네요. ㅇ_ㅇ''

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

jungwook의 이미지

x86-64 has a different assembly langauge and a different ABI.

이렇게 나오네요.

32bit 에서 한번 실행해봐야겠네요. ㅡㅡ;

답변 감사합니다.

pastime의 이미지

첫 번째 asm 문장에서 argument가 빠져있네요..

crowlead의 이미지

제가 잘 못찾아봐서 그런지는 몰라도.. 어셈블리어 관련 자료 찾는게 어려운거 같아요.
------

------

swirlpotato의 이미지

#include

int main()
{
int a = 1;
int number = 8668;
char *str = "a = %d, number = %d\n";

__asm__ __volatile(
"pushl %0\n\t"
"pushl %1\n\t"
"pushl %2\n\t"
"call printf\n\t"
:
:"m" (number), "m" (a), "m" (str)
);

__asm__ __volatile("movl $4, %%eax\n\t"
"movl $5, %%ebx\n\t"
:
:
:"eax", "ebx");
return 0;
}

틀리신 점이 %0 %1 %2 이건 output input argument 부분에 적은 순서대로 됩니다.
적지 않으셨으니 %0 ~ %2를 찾지 못했으니 에러죠.
사실 \n\t은 안쓰셔도 되는데 소스로 뽑았을 때 정렬때문에 그냥 \n\t합니다.

그리고 스택에 넣으실 때 반대로 넣으시구요.

그리고 kldp wiki 보시면 인라인 어셈블리 관련 문서가 있습니다.

익명 사용자의 이미지

#include int main() { 님 말대로 어셈 명령 밑에 저런 게 있어야 하고요,
하나 더 붙히자면 __cdecl 함수 호출 후에 스택을 정리햐셔야 합니다
pushl 3번으로 바뀐 스택을 popl이나 esp에 조작을 가해 원상태로 돌려야 하죠

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.