Assembly 汇编中的增量操作

Assembly 汇编中的增量操作,assembly,nasm,Assembly,Nasm,我有以下汇编代码: segment .data sayi db 1 segment .text global _start _start: mov ecx, sayi inc ecx mov [sayi], ecx mov eax, 4 ;system call number for output(sys_write) mov ebx, 1 ;def

我有以下汇编代码:

segment      .data
     sayi db 1

segment      .text
     global _start

_start: 

     mov ecx, sayi
     inc ecx
     mov [sayi], ecx

     mov eax, 4             ;system call number for output(sys_write)
     mov ebx, 1             ;default output device
     mov ecx, sayi          ;message to write
     mov edx, 4             ;message length   
     int 0x80               ;call kernel

     mov eax,1              ;The system call for exit (sys_exit)
     mov ebx,0              ;Exit with return code of 0 (no error)
     int 0x80               ;call kernel 

我只是试着增加一个数字,然后打印出来。这将成功编译,但不会将任何内容打印到屏幕上。我在这里遗漏了什么?

您的代码有几个问题:

 mov ecx, sayi
 inc ecx
 mov [sayi], ecx
第一行将
sayi
的地址复制到
ecx
,而不是
sayi
处的值。要获得该值,您需要编写
mov ecx,[sayi]
。 另一个问题是您在
sayi
,但您试图访问它,就像它是一个
dword
(4字节)一样。要保留
dword
,您应该使用
dd
而不是
db

然后就是你的打印问题了。
sys\u write
syscall将
ecx
指向的任何内容打印为字符序列。它不做从整数到字符串的任何转换,所以这是您必须自己做的事情。 下面是一个NASM实现的示例,我为前面的问题编写了这样一个转换:

; Input:
; EAX = integer value to convert
; ESI = pointer to buffer to store the string in (must have room for at least 10 bytes)
; Output:
; EAX = pointer to the first character of the generated string
int_to_string:
  add esi,9
  mov byte [esi],0

  mov ebx,10         
.next_digit:
  xor edx,edx         ; Clear edx prior to dividing edx:eax by ebx
  div ebx             ; eax /= 10
  add dl,'0'          ; Convert the remainder to ASCII 
  dec esi             ; store characters in reverse order
  mov [esi],dl
  test eax,eax            
  jnz .next_digit     ; Repeat until eax==0
  mov eax,esi
  ret

或者,范围为0..9的数字可以通过简单地向其添加
'0'
转换为数字,然后通过将数字地址放入
ecx
edx
中的
1
打印
sys\u write

sys\u write
难道不希望得到一个字符串吗?我只需要增加一个数字。一个字节就足够了。说到字符,我想我不需要在这里进行数字转换,因为“0”加1就是“1”,对吗?如果要将变量保留为一个字节,则需要将其作为一个字节访问,例如
mov cl,[sayi]
。我在您的代码中没有看到任何
'0'
'1'
,但是您可以通过添加
'0'
将0..9范围内的数字转换为相应的字符。我通过@Damien\u异教徒的评论解决了这个问题,但感谢您的努力。作为初学者编写程序集是令人讨厌的:)我的回答中已经包含了关于期望字符串地址的部分:“
sys\u write
syscall将
ecx
指向的任何内容打印为字符序列。它不做从整数到字符串的任何转换,所以这是您必须自己做的事情。”