在AT& T IA-32 Linux汇编程序(气体)上拆分字符串

时间:2008-12-30 02:13:19

标签: c linux assembly x86 gas

.section .data

astring: .asciz "11010101"
format: .asciz "%d\n"

.section .text
.globl _start

_start:

xorl %ecx, %ecx

movb astring(%ecx,1), %al
movzbl %al, %eax

pushl %eax
pushl $format
call printf
addl $8, %esp


movl $1, %eax
movl $0, %ebx
int $0x80

假设我想打破.asciz字符串1101011并获得它的第一个。我该怎么办呢?上面的代码不起作用,它打印49或其他。

2 个答案:

答案 0 :(得分:2)

printf的转化说明符从%d更改为%c,以打印字符而不是其ascii值。

答案 1 :(得分:1)

4年后。 我正在学习使用GNU Assembler在asm中编程。 我这样做是为了练习:

.section .rodata
.LC0:
.string "This is the number: %d \n"
.data
.type str, @object
str:
.long .LC0

.section .text
.globl main
.type main, @function
.extern printf
main:
push %ebp
movl %esp, %ebp
andl $-16, %esp
subl $12, %esp
movl $2600, 4(%esp)
movl str, %edx
# Simple printf
movl %edx, %eax
movl %eax, (%esp)
call printf

# putchar
# for loop
movl $0, -4(%ebp)
jmp .check_for
.for_loop:
movl -4(%ebp), %eax
movl str, %edx
leal (%edx, %eax), %eax
movzbl (%eax), %eax
movsbl %al, %eax
movl %eax, (%esp)
call putchar
addl $1, -4(%ebp)
.check_for:
cmp $0x00, (%esp)
jnz .for_loop
leave
ret