본문 바로가기
코스웨어/15년 스마트컨트롤러

20151201_안향진_ARM어셈블리_3__WINSOCK_2

by 알 수 없는 사용자 2015. 12. 2.
728x90
반응형

==========================================================================================

ARM 어셈블리

==========================================================================================


<ARM 어셈블리>


클럭 : 현재 데이터의 사용 유무를 클럭 신호로..

플립플롭



 =mnemonic

숫자를 어떻게 해석 할 것인가 : 명령, 숫자


12  | 34 | 56 | 78

주소


int * A

char * B

short * C


*A = 78563412

*B = 12

*C = 3412




=cross compile 환경


=armcc

(ads상용컴파일러)


http://www.atmel.com/tools/SAM7S-EK.aspx


Tools -> Evaluation Kit

샘플보드


GNU ( Cstartup.s )

ADS => IAR(버전 업): 60일 체험판



=

C:\Program Files\ARM\ADSv1_2\Bin\IDE.exe













'







=DebugRel Settings
















-info totals -ro-base 0x00000000 -rw-base 0x00200000 -first Cstartup.o(reset)





-info totals -ro-base 0x00000000 -map -rw-base 0x00200000 -list 20151201.map -first Cstartup.o(reset) -info sizes







=탭 간격 조절







=MAKE









=WARNING 해결









=map/bin파일 생성




ELF - OBJ (LIB)

dwarf

COFF

OBJ

WINDOWS : PE파일 형식

리눅스 : ELF파일 형식





ld:linker

s:scripter


=> 메모리의 주소 구성을 원하는 대로




=AVR MAKEFILE에서 MAP 파일 생성 옵션 부분 찾아

<AVR>

#---------------- Linker Options ----------------

#  -Wl,...:     tell GCC to pass this to linker.

#    -Map:      create map file

#    --cref:    add cross reference to  map file

LDFLAGS = -Wl,-Map=$(TARGET).map,--cref

LDFLAGS += $(EXTMEMOPTS)

LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))

LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)

#LDFLAGS += -T linker_script.x


=ARM MAKEFILE에 추가

<ARM> 

        LDFLAGS+=-nostartfiles -Wl,--cref

==>>   LDFLAGS+=-nostartfiles -Wl,-Map=test.map,--cref




<test.map>


 Memory Configuration

Name             Origin             Length             Attributes
*
default*        0x00000000         0xffffffff

Linker script and memory map

LOAD /cygdrive/c/program files/gnuarm/bin/../lib/gcc/arm-elf/
4.1.0/../../../../arm-elf/lib/libc.a
LOAD /cygdrive/c/program files/gnuarm/bin/../lib/gcc/arm-elf/
4.1.0/libgcc.a
LOAD cstartup.o
LOAD lowlevel.o
LOAD main.o
START GROUP
LOAD /cygdrive/c/program files/gnuarm/bin/../lib/gcc/arm-elf/
4.1.0/libgcc.a
LOAD /cygdrive/c/program files/gnuarm/bin/../lib/gcc/arm-elf/
4.1.0/../../../../arm-elf/lib/libc.a
END GROUP

.text           
0x00100000      0x264
                0x00100000                _stext = .
 *(.text)
 .text          
0x00100000       0xd0 cstartup.o
                
0x00100000                reset_handler
 .text          
0x001000d0      0x110 lowlevel.o
                
0x001001d4                default_fiq_handler
                
0x001000d0                lowlevel_init
                
0x001001d8                default_irq_handler
                
0x001001dc                default_spurious_handler
 .text          
0x001001e0       0x84 main.o
                
0x001001e0                main
 *(.rodata)
 *(.rodata*)
                
0x00100264                . = ALIGN (0x4)
                
0x00100264                _etext = .

.glue_7         
0x00100264        0x0

.glue_7t        0x00100264        0x0

.data           0x00200000       0x70 load address 0x00100264
                0x00200000                _sdata = .
 *(.vectors)
 .vectors       
0x00200000       0x70 cstartup.o
 *(.data)
                
0x00200070                _edata = .

.bss            
0x00200070        0x0
                0x00200070                . = ALIGN (0x4)
                
0x00200070                _sbss = .
 *(.bss)
                
0x00200070                _ebss = .
                
0x00200070                end = .
Address of section .text set to 
0x100000
Address of section .data set to 0x200000
OUTPUT(at91sam7s_flash_wc.elf elf32-littlearm)

.debug_line     
0x00000000      0x115
 .debug_line    0x00000000       0x84 cstartup.o
 .debug_line    
0x00000084       0x55 lowlevel.o
 .debug_line    
0x000000d9       0x3c main.o

.debug_info     
0x00000000      0x15a
 .debug_info    
0x00000000       0x60 cstartup.o
 .debug_info    
0x00000060       0x96 lowlevel.o
 .debug_info    
0x000000f6       0x64 main.o

.debug_abbrev   
0x00000000       0xbf
 .debug_abbrev  
0x00000000       0x10 cstartup.o
 .debug_abbrev  
0x00000010       0x58 lowlevel.o
 .debug_abbrev  
0x00000068       0x57 main.o

.debug_aranges  
0x00000000       0x68
 .debug_aranges
                
0x00000000       0x28 cstartup.o
 .debug_aranges
                
0x00000028       0x20 lowlevel.o
 .debug_aranges
                
0x00000048       0x20 main.o

.debug_frame    
0x00000000       0x74
 .debug_frame   0x00000000       0x50 lowlevel.o
 .debug_frame   
0x00000050       0x24 main.o

.debug_loc      
0x00000000       0x32
 .debug_loc     0x00000000       0x13 lowlevel.o
 .debug_loc     
0x00000013       0x1f main.o

.debug_pubnames
                
0x00000000       0x8c
 .debug_pubnames
                
0x00000000       0x71 lowlevel.o
 .debug_pubnames
                
0x00000071       0x1b main.o

.debug_str      
0x00000000       0xce
 .debug_str     
0x00000000       0xbb lowlevel.o
                                 
0xc8 (size before relaxing)
 .debug_str     
0x000000bb       0x13 main.o
                                 
0x61 (size before relaxing)

.comment        
0x00000000       0x24
 .comment       0x00000000       0x12 lowlevel.o
 .comment       
0x00000012       0x12 main.o

Cross Reference Table

Symbol                                            File
_ebss                                             cstartup.o
_edata                                            cstartup.o
_etext                                            cstartup.o
_sbss                                             cstartup.o
_sdata                                            cstartup.o
default_fiq_handler                               lowlevel.o
default_irq_handler                               lowlevel.o
default_spurious_handler                          lowlevel.o
lowlevel_init                                     lowlevel.o
                                                  cstartup.o
main                                              main.o
                                                  cstartup.o
reset_handler                                     cstartup.





=-S 옵션으로 C파일 -> 어셈블리 파일


=armar -r .lib x.o, x.o



=C:\Program Files\ARM\ADSv1_2\Lib\armlib







=C:\Program Files\ARM\ADSv1_2\Bin







==========================================================================================

WINSOCK

==========================================================================================



<WINSOCK>

-윈도우 시리얼

-윈도우 패킷 캡쳐


http://www.joinc.co.kr/modules/moniwiki/wiki.php/Site/win_network_prog/doc/winsock_basic



=채팅

*동작안됨

<smart.h>

#ifndef __SMART_H__
#define
 __SMART_H__

#include
 <stdio.h>
#include
 <winsock2.h> 

//#pragma comment(lib, "wsock32.lib")

#pragma
 comment(lib, "ws2_32.lib"// 최신

#define
 PORT 7777
//#define IP "192.168.0.169"

#define
 IP "192.168.56.1"

#define
 NIC_NAME_MSG  (9+2)
#define
 BUF_SIZE    (255)
#define
 MSG_SIZE    (BUF_SIZE + 1 + NIC_NAME_MSG)
#define
 MSG_END     "\x01\x02\x03"//\x뒤는 아스키코드 헥사번호로 인식 //"quit"
#define
 MAX_USER  2
#define
 NIC_NAME_SIZE  9
#endif
 //__SMART_H__



<server.c>

#include "smart.h"

int
 main(int argc, char *argv[])
{
  WSADATA wsaData;
  SOCKET sSock;
  SOCKET cSock[MAX_USER+
1];
  SOCKADDR_IN sAddr;
  
int iStatus;
  
int iLen;
  
char cBuffer[BUF_SIZE];
  
char cMsg[MSG_SIZE]; 
  fd_set fdRead;
  
char cNick[MAX_USER][NIC_NAME_SIZE];
  
unsigned int uiUser;
  
int MSock;
  
unsigned int uiCnt;
  
unsigned int uiCnt2;
  
unsigned int  uiSockLen = sizeof(SOCKADDR);
  
  
if(WSAStartup(MAKEWORD(2,2),&wsaData)!= 0)
  {
    printf(
"WSAStartup error\n");
    
return 0;
  }

  sSock 
= socket(AF_INET, SOCK_STREAM, 0);
  
if(INVALID_SOCKET == sSock)
  {
    printf(
"Invalid socket error\n");
    WSACleanup(); 
    
return 1;
  }

  memset(
&sAddr, 0sizeof(SOCKADDR_IN));
  sAddr.sin_family 
= AF_INET;
  sAddr.sin_port 
= htons(PORT);
  sAddr.sin_addr.S_un.S_addr 
= inet_addr(IP);
    
  iStatus 
= bind( sSock, (SOCKADDR*)&sAddr, sizeof(SOCKADDR_IN) ); 
  
if(SOCKET_ERROR == iStatus)
  {    
    printf(
"bind error\n");
    closesocket(sSock);
    WSACleanup(); 
    
return 2;
  }

  
if( SOCKET_ERROR == listen( sSock, 5 )) 
  {     
    printf(
"listen Error\n"); 
    closesocket(sSock);
    WSACleanup(); 
    
return 3
  }
  uiUser 
= 0;
  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET((
int)sSock, &fdRead);
    MSock 
= (int)sSock;
    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      FD_SET((
int)cSock[uiCnt], &fdRead);
      
if(MSock < (int)cSock[uiCnt])
      {
        MSock 
= (int)cSock[uiCnt];
      }
    }
    
    select(MSock+
1&fdRead, 000);
    
if(0 != FD_ISSET((int)sSock, &fdRead))
    {
  
      cSock[uiUser] 
= accept(sSock, (SOCKADDR*)&sAddr, &uiSockLen);
      
if(cSock[uiUser] < 0)
      {
        perror(
"accept() error : ");
        
continue;
      }
    
      
if(MAX_USER <= uiUser)
      {
        
//read(cSock[uiUser], cBuffer, NIC_NAME_SIZE);
        recv( cSock[uiUser], cMsg, NIC_NAME_SIZE,
0 ); 

        sprintf(cMsg, 
"Server is FULL.");
        
//write(cSock[uiUser], cMsg, sizeof(cMsg));
        
//write(cSock[uiUser], MSG_END, sizeof(MSG_END));
        send( cSock[uiUser], cMsg,  
sizeof(cMsg), 0 ); 
        send( cSock[uiUser], MSG_END, 
sizeof(MSG_END), 0 ); 
        closesocket(cSock[uiUser]);
        printf(
"Server is FULL : [%s]\n", cBuffer);
        printf(
"client IP : [%s]\n", inet_ntoa(sAddr.sin_addr)); //client IP
        
continue;
      }
  
          recv( cSock[uiUser], cNick[uiUser], NIC_NAME_SIZE,
0 ); 
          printf(
"test5 cNick[uiUser]: %s\n", cNick[uiUser]);
          printf(
"incoming client: [%s]\n", cNick[uiUser]); //누군가 접근중이다.
          printf(
"client IP : [%s]\n", inet_ntoa(sAddr.sin_addr)); //client IP
          
//write(cSock[uiUser], "Welcome!", sizeof("Welcome!"));        
          send( cSock[uiUser], 
"Welcome!",  sizeof("Welcome!"), 0 ); 
          sprintf(cMsg, 
"[%s]is Entered.", cNick[uiUser]);
          
for(uiCnt2 = 0; uiCnt2 < uiUser ;++uiCnt2)
          {
            
//write(iCSock[uiCnt2], cMsg, MSG_SIZE);
            send( cSock[uiCnt2], cMsg,  MSG_SIZE, 
0 ); 
          }
          ++uiUser;
          printf(
"%s\n", cMsg);

      }
    

/*
    if(0 !
= FD_ISSET(0, &fdRead))
    {
      iLen 
= read(0, cBuffer, BUF_SIZE);
      //iLen 
= recv( 0, cBuffer, BUF_SIZE,0 ); 
      if(0 
== iLen)
      {
        for(uiCnt 
= 0; uiCnt < uiUser ;++uiCnt)
        {
          sprintf(cMsg, "Server is ENDING.");
          //write(iCSock[uiCnt], cMsg, sizeof(cMsg));
          //write(iCSock[uiCnt], MSG_END, sizeof(MSG_END));
          send( cSock[uiUser], cMsg,  sizeof(cMsg), 0 ); 
          send( cSock[uiUser], MSG_END, sizeof(MSG_END), 0 );
        }
        break;
      }
      cBuffer[iLen-1] 
= 0;
      sprintf(cMsg, "Notice: [%s]", cBuffer);
      for(uiCnt 
= 0; uiCnt < uiUser ;++uiCnt)
      {
        //write(cSock[uiCnt], cMsg, MSG_SIZE);
        send( cSock[uiCnt], cMsg, MSG_SIZE, 0 );
      }
      printf("%s\n", cMsg);
    }
*/

    
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
    {
      
if(0 != FD_ISSET((int)cSock[uiCnt], &fdRead))
      {
        
//iRet = read(cSock[uiCnt], cMsg, MSG_SIZE);
        iLen 
= recv( cSock[uiCnt], cMsg, MSG_SIZE,0 );
        
//if(0 == strncmp(MSG_END, cMsg+strlen(cNick[uiCnt])+2, strlen(MSG_END)))
        
if(
          (
0 == strncmp(MSG_END, cMsg, sizeof(MSG_END)))
        
//  ||
        
//  (0 == iRet)
          )
        {
          sprintf(cMsg, 
"[%s]님이 퇴장하셨습니다.", cNick[uiCnt]);
          closesocket(cSock[uiCnt]);
          --uiUser;
          cSock[uiCnt] 
= cSock[uiUser];
          memcpy(cNick[uiCnt],cNick[uiUser], NIC_NAME_SIZE);
        }  
        
for(uiCnt2 = 0; uiCnt2 < uiUser ;++uiCnt2)
        {
          
//write(cSock[uiCnt2], cMsg, MSG_SIZE);
          send( cSock[uiCnt2], cMsg,  
sizeof(cMsg), 0 ); 
        }

        printf(
"%s\n", cMsg);
      }
    }    
  }
  
  
for(uiCnt = 0; uiCnt < uiUser ;++uiCnt)
  {
    closesocket(cSock[uiCnt]);
  }
  closesocket(sSock);
  WSACleanup();
  
  
return 0;
}



<client.c>

#include "smart.h"

int
 main(int argc, char *argv[])
{
  WSADATA wsaData;
  SOCKET cSock;
  SOCKADDR_IN sAddr;
  
int iStatus;
  
int iLen;
  fd_set fdRead;
  
char cBuffer[BUF_SIZE];
  
char cMsg[MSG_SIZE];
  
char cNick[NIC_NAME_SIZE];
  
int iFd;
  
unsigned short usPort = PORT;
  
  
if(3 == argc)
  {
    iFd 
= atoi(argv[2]);
    
if(1024 < iFd)
    {
      
if(65535 > iFd)
      {
        usPort 
= iFd;
      }
    }
  }
  printf(
"PORT: %d\n", usPort);
  
if(NULL == argv[1])
  {
    printf(
"IP: %s\n", IP);
  }
  
else
  {
    printf(
"IP: %s\n", argv[1]);
  }
  
while(1)
  {
    printf(
"Input Nickname: ");
    fflush(stdout);
    iLen 
= read(0, cNick, NIC_NAME_SIZE);
    printf(
"iLen : %d\n",iLen);
    
//iLen = recv( 0, cNick, NIC_NAME_SIZE,0 ); 
    
if(2 > iLen) // ctrl+d : 0 enter : 1
    {
      
continue;
    }
    cNick[iLen - 
1= 0;
    
break;
  }
  
  
if(WSAStartup(MAKEWORD(2,2),&wsaData)!= 0)
  {
    printf(
"WSAStartup error\n");
    
return 0;
  }

  cSock 
= socket(AF_INET, SOCK_STREAM, 0);
  
if(INVALID_SOCKET == cSock)
  {
    printf(
"Invalid socket error\n");
    WSACleanup(); 
    
return 1;
  }

  memset(
&sAddr, 0sizeof(SOCKADDR_IN));
  sAddr.sin_family 
= AF_INET;
  sAddr.sin_port 
= htons(usPort);
  
if(NULL == argv[1])
  {
    sAddr.sin_addr.S_un.S_addr 
= inet_addr(IP);
  }
  
else
  {
    sAddr.sin_addr.S_un.S_addr 
= inet_addr(argv[1]);
  }
    
  iStatus 
= connect( cSock, (SOCKADDR*)&sAddr, sizeof(SOCKADDR_IN) ); 
  
if(SOCKET_ERROR == iStatus)
  {    
    printf(
"connect error\n");
    closesocket(cSock);
    WSACleanup(); 
    
return 2;
  }
  send( cSock, cNick,  NIC_NAME_SIZE, 
0 ); 
  printf(
"Nick : [%s]\n", cNick);
  
while(1)
  {
    FD_ZERO(
&fdRead);
    FD_SET(
0&fdRead);
    FD_SET((
int)cSock, &fdRead);

    select((
int)cSock+1&fdRead, 000);
    
    
if(0 != FD_ISSET((int)cSock, &fdRead))
    {
      
//iRet = read(iFd, cMsg, MSG_SIZE);//iFd:네트워크
      iLen 
= recv( cSock, cMsg, MSG_SIZE,0 ); 
      
if(0 >= iLen)
      {
        printf(
"Server Error.\n");
        
break;
      }
      
if(0 == strncmp(MSG_END, cMsg, sizeof(MSG_END)))
      {
        
break;
      }
      printf(
"%s\n", cMsg);
    }
    
    
if(0 != FD_ISSET(0&fdRead))
    {
      iLen 
= read(0, cBuffer, BUF_SIZE);
      
//iLen = recv( 0, cBuffer, BUF_SIZE,0 ); 
      
if(0 == iLen)
      {
        
//write(iFd, MSG_END, sizeof(MSG_END));
        send( cSock, MSG_END, 
sizeof(MSG_END), 0 ); 
        
break;
      }
      cBuffer[iLen - 
1= 0;
      sprintf(cMsg, 
"[%s]%s", cNick, cBuffer);
      
//write(iFd, cMsg, MSG_SIZE);
      send( cSock, cMsg, MSG_SIZE, 
0 ); 
      printf(
"[Send MSG] : [%s]\n", cBuffer);

    }
    


  }

  closesocket(cSock);
  WSACleanup();
  
  
return 0;
}




728x90