我是一个使用高级语言的开发人员,在业余时间学习汇编语言。请参阅下面的NASM程序:
section .data
section .bss
section .text
global main
main:
mov eax,21
mov ebx,9
add eax,ebx
mov ecx,eax
mov eax,4
mov ebx,1
mov edx,4
int 0x80
push ebp
mov ebp,esp
mov esp,ebp
pop ebp
ret下面是我使用的命令:
ian@ubuntu:~/Desktop/ nasm /Program4$ NASM -f elf -o asm.o SystemCalls.asm ian@ubuntu:~/Desktop/NASM/Program4$ gcc -o程序asm.o ian@ubuntu:~/Desktop/NASM/Program4$ ./program
我没有收到任何错误,但是终端没有输出任何内容。我使用以下链接来确保寄存器包含正确的值:http://docs.cs.up.ac.za/programming/asm/derick_tut/syscalls.html
发布于 2013-09-17 02:45:08
您必须将整数值转换为字符串,才能使用sys_write (Syscall4)打印它。转换可以像这样完成(未经测试):
; Converts the integer value in EAX to a string in
; decimal representation.
; Returns a pointer to the resulting string in EAX.
int_to_string:
mov byte [buffer+9],0 ; add a string terminator at the end of the buffer
lea esi,[buffer+9]
mov ebx,10 ; divisor
int_to_string_loop:
xor edx,edx ; clear edx prior to dividing edx:eax by ebx
div ebx ; EAX /= 10
add dl,'0' ; take the remainder of the division and convert it from 0..9 -> '0'..'9'
dec esi ; store it in the buffer
mov [esi],dl
test eax,eax
jnz int_to_string_loop ; repeat until EAX==0
mov eax,esi
ret
buffer: resb 10发布于 2015-02-12 19:14:55
汇编语言编程需要了解ASCII码和一些基本的转换例程。例如:十六进制到十进制,十进制到十六进制都是很好的例程,可以保存在某些存储空间中。没有寄存器可以按原样打印,您必须转换(大量)。更有帮助的是:ASCII0不打印任何东西,但是一些文本编辑器( kde linux中的kate)会在屏幕上显示一些东西(一个正方形或...)。在高级语言中,如C和C++,它用于指示空指针和字符串的结尾。也可用于计算字符串长度。10是行尾。根据Linux或Windows的不同,也会有回车符(Linux)或不会有回车符(Windows/Dos)。13是回车符1B是ESC键(Linux用户现在会更多地了解这一点) 255是一个硬回车符,我从来不知道它为什么好用,但它一定有它的目的。查看整个列表的http://www.asciitable.com/。
发布于 2016-04-27 23:41:54
将整数值转换为字符串。在这里,我使用宏打包和解包将整数转换为字符串,使用宏解压缩将整数转换为字符串,反之亦然
%macro write 2
mov eax, 4
mov ebx, 1
mov ecx, %1
mov edx, %2
int 80h
%endmacro
%macro read 2
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,%2
int 80h
%endmacro
%macro pack 3 ; 1-> string ,2->length ,3->variable
mov esi, %1
mov ebx,0
%%l1:
cmp byte [esi], 10
je %%exit
imul ebx,10
movzx edx,byte [esi]
sub edx,'0'
add ebx,edx
inc esi
jmp %%l1
%%exit:
mov [%3],ebx
%endmacro
%macro unpack 3 ; 1-> string ,2->length ,3->variable
mov esi, %1
mov ebx,0
movzx eax, byte[%3]
mov byte[%2],0
cmp eax, 0
jne %%l1
mov byte[%2],1
push eax
jmp %%exit2
%%l1:
mov ecx,10
mov edx,0
div ecx
add edx,'0'
push edx
inc byte[%2]
cmp eax, 0
je %%exit2
jmp %%l1
%%exit2:
movzx ecx,byte[%2]
%%l2:
pop edx
mov [esi],dl
inc esi
loop %%l2
%endmacro
section .data ; data section
msg1: db "First number : " ;
len1: equ $-msg1 ;
msg2: db "Second number : " ;
len2: equ $-msg2 ;
msg3: db "Sum : " ;
len3: equ $-msg3 ;
ln: db 10
lnl: equ $-ln
var1: resb 10
var2: resb 10
str1: resb 10
str2: resb 10
ans: resb 10
ansvar: resb 10
ansl: db ''
l1: db ''
l2: db ''
section.text ;code
global _start
_start:
write msg1,len1
read str1,10
pack str1,l1,var1
write msg2,len2
read str2,10
pack str2,l2,var2
mov al,[var1]
add al,[var2]
mov [ansvar],al
unpack ans,ansl,ansvar
write msg3,len3
write ans,10
write ln,lnl
mov ebx,0 ; exit code, 0=normal
mov eax,1 ; exit command to kernel
int 0x80 ; interrupt 80 hex, call kernel要进行汇编、链接和运行:
nasm -f elf add.asm
ld -s -o add add.o
./add https://stackoverflow.com/questions/18834760
复制相似问题