开发者

Creating a buffer in a nasm macro?

As you might guess, I'm new to this (both nasm and assembly, though I've done some basic assembly before).

I'm trying to create a function that prints integers to standard output. Using non-reusable code (where the number to be printed is static), I've succeeded... However, for obvious reasons, I want it to take the number to print as an argument.

I'm unsure whether a macro or a function is best for me, and I can't find much on the topic of NASM macros at all. I've read the manual quite carefully, but it's not enough.

Anyway, I've tried to do this with a NASM macro, as I created another one that prints strings with success that way.

I've narrowed down the problematic code to this:

%macro crash 1
jmp %%endstr
%%str: db %1,0x0a
%%endstr:
mov [%%st开发者_运维技巧r], byte 0x16 <<< this crashes (segmentation fault)
%endmacro

section .text
global _start
_start:
crash "abc"

It looks like anything that uses brackets on the buffer crashes, and I can only assume I'm doing it wrong.

What I want the above to do is to overwrite the first byte in %%str with another byte value. More precisely, I need to write a string to a buffer byte-by-byte backwards; I (try to) do this with a loop, where I do

mov [%%str+rcx], dl
dec rcx

until rcx is 0.

If I shouldn't use macros for this, please enlighten me!

I intend to save the function in a mini-library for later use as well, so it should be easy to pop it in to any NASM project.

As the topic and tags say, all this is under Linux/amd64.


You can't do that in the code segment because it's read-only. You should declare str in the @data segment, then you'll be fine. And, just like @user786653 said, "You should make this a function, having macros spread internal state all around your code is bad style (even for assembler!)".


for in a section .data

; use name and number of bytes

%macro BUFFER 2
%1:
   .start: times %2 db 0
   .end:
   .length: equ %1.end-%1.start
%endmacro

for in section .bss

; use name and number of bytes

%macro BUFFER 2
%1:
    .start: resb %2
    .end:
    .length: equ %1.end-%1.start
%endmacro

problem here is that is we want to know the last byte of the buffer then it is buffer.end- 1 a possible solution but I didn't try it yet:

%macro BUFFER 2
%1:
    .start: times %2-1 db 0
    .end: db 0
    .length: equ %1.end-%1.start
%endmacro

idem for .bss section

%macro BUFFER 2
%1:
    .start: resb %2-1
    .end: resb 1
    .length: equ %1.end-%1.start
%endmacro
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜