gcc 인라인 어셈에서 잘 이해가 안가는 문법

barrios의 이미지

Intel Assem과는 좀 다른 부분 중에 이해가 잘 안가는 부분이 있어 관련 문서
를 봐도 영어가 딸려서 그런지 잘 이해가 안가네요.. 쩝..쩝..

 int main (void) 
{
      int operand1, operand2, sum, accumulator;
    
      operand1 = rand (); operand2 = rand ();
      
      __asm__ ("movl %1, %0\n\t"
      	       "addl %2, %0"
	       : "=r" (sum) /* output operands */
	       : "r" (operand1), "r" (operand2) /* input operands */
	       : "0"); /* clobbered operands */
      
      accumulator = sum;
      
      __asm__ ("addl %1, %0\n\t"
	       "addl %2, %0"
	       : "=r" (accumulator)
	       : "0" (accumulator), "g" (operand1), "r" (operand2)
	       : "0");

      return accumulator;
    }

여기서 %0 %1 %2 이것이 모죠 ?? 같은 레지스터 사용을 피하기 위한 것이
라고 써져 있던 것 같은데..잘 이해가 안가서요.

그 다음 input, output, 과 clobbered operands 란 것이 모죠 ??
문법 희안하네요..^^ 임의의 레지스터를 알아서 선택하라는 것인가요 ?
r은 general, g 는 any register 등등.. 이렇게 되어 있는 것 같은데..정확한
개념좀 부탁드립니다.

Tony의 이미지

__asm__ ("movl %1, %0\n\t"
"addl %2, %0"
"=r" (sum) /* output operands */
"r" (operand1), "r" (operand2) /* input operands */
"0"); /* clobbered operands */

"=r" (sum) == %0
"r" (operand1) == %1
"r" (operand2 == %2

RTFM

barrios의 이미지

사소한 질문 인것 같아 조심스레 올려 봅니다. 나름데로 관련 자료를 읽어봐도 도대체 무슨 소리인지 잘 이해가 안가서 이렇게 다시 질문 드립니다. 위에 님께서 설명해주신 내용은 이해가 가는데요..그렇다면 이것은

#include <stdio.h>

int main(void) {
	int foo=10,bar=15;
	
	__asm__ __volatile__ ("addl 	%%ebxx,%%eax" 
		: "=eax"(foo) 		// ouput
		: "eax"(foo), "ebx"(bar)// input
		: "eax"			// modify
	);
	printf("foo+bar=%d\n", foo);
	return 0;
}

제 나름데로 해석을 해보겠습니다.

1) output을 foo 변수에 넣어라.(그러데 output이란 것이 모죠 ?? 저 어셈 코드에서output이라 함은 ? ebx 값과 eax값을 더해서 eax에 넣어라 아닙니까 ?
그렇다면 output은 더한 eax값이란 말입니까 ??

2) input은 foo 와 bar인데.. input이 왜 2개죠 ?
2) 마지막 eax 값은 modify 란 것 이건 또 모죠 ? 그냥 변할 수 있다라는 건가요 ? ?? 쩝쩝. Linux inline assembly 참 신기하네요..

kslee80의 이미지

GCC inline Assembly 라는 문서가 있습니다...
각 인자의 역할과 왜 필요한지까지 아주 자세하게 설명되어 있답니다.
영문 문서가 아니라, 영문 문서를 번역해 놓은 한글문서입니다.

RTFM

barrios의 이미지

감사합니다.관련 문서 찾아서 잘 봤습니다.

혹시 저와 같이 GCC inline assem 처음 하시는 분들을 위해..

http://doc.kldp.org/KoreanDoc//html/GCC_Inline_Assembly-KLDP/

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.