Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- solidity
- libray
- NaughtCoin
- Oracle Cloud
- coin flip
- Block
- hard fork
- Ethererum
- approve
- audit
- ethereum
- Wargame
- transaction
- byte code
- web3
- openzepplin
- tx.origin
- TransferFrom
- Coin
- ethernaut
- Smart contract
- EVM
- Assembly
- writeup
- syntax
- chain reorganization
- web assembly
- soft fork
- ethereum virtual machine
- secureum
Archives
- Today
- Total
c0mpos3r
NULL terminating bytes 본문
그렇다면 왜 msg2에서 스프린트 함수를 한 번만 호출했는데 두 번째 메시지가 두 번 인쇄된 걸까?
사실 한 번만 인쇄되었다. 두 번째 sprint 호출에 주석을 달아보면 무슨 말인지 알 수 있다.
출력은 두 메시지 문자열 모두이다.
하지만 이것이 어떻게 가능할까?
문자열을 제대로 종료하지 않았기 때문이다. 어셈블리에서 변수는 메모리에 차례로 저장되므로 msg1 변수의 마지막 바이트는 msg2 변수의 첫 번째 바이트 바로 옆에 있다.
문자열 길이 계산은 0 바이트를 찾고 있으므로 msg2 변수가 0바이트로 시작하지 않는 한 동일한 문자열인 것처럼 계속 계산한다(어셈블리에 관한 한 동일한 문자열이다.) 따라서 문자열 뒤에 0바이트 또는 0h를 넣어 어셈블리가 카운터를 중지할 위치를 알 수 있도록 해야한다.
참고: 프로그래밍에서 0h는 널 바이트를 나타내며 문자열 뒤에 널 바이트는 메모리에서 문자열이 끝나는 위치를 어셈블리에게 알려준다.
helloworld-inc.asm
%include 'functions.asm'
SECTION .data
msg1 db 'Hello, brave new world', 0Ah, 0h ; 널 종료 바이트에 주목하자
msg2 db 'This is how we recycle in NASM', 0Ah, 0h ; 널 종료 바이트에 주목하자
SECTION .text
global _start
_start:
mov eax, msg1
call sprint
mov eax, msg2
call sprint
call quit
Result
~$ nasm -f elf helloworld-inc.asm
~$ ld -m elf_i386 helloworld-inc.o -o helloworld-inc
~$ ./helloworld-inc
Hello, brave new world!
This is how we recycle in NASM.
이 글은 https://asmtutor.com/#lesson5의 자료를 참고하여 작성되었습니다.
이 글은 상업적 목적이 아닌 개인이 공부한 내용을 정리하기 위한 글입니다.
'Develop > Assembly' 카테고리의 다른 글
| Linefeeds (1) | 2024.07.04 |
|---|---|
| External include files (0) | 2024.07.04 |
| Subroutines (0) | 2024.07.04 |
| Calculate string length (0) | 2024.07.04 |
| Proper program exit (0) | 2024.07.04 |