如何使用基本寻址模式将字符保存到变量中



我是Assembly的新手,正在学习基础知识,但我在这方面已经坚持了一段时间,不知道如何克服它。下面的代码可以工作,但不使用所需的基本寻址模式。

我必须使用基本寻址模式将String中的第五个字符复制到变量N中。我的方法(不使用基址寻址模式(是使用带偏移的基址。我不确定我将如何实现这种做基础寻址模式,任何帮助都将不胜感激。

;Initialized data
section .data
msg1: db "Input a string: ",10 
msg1_L: equ $-msg1      ;calculate size of msg1
n_line DB 0AH,0DH,"$"   
;Uninitialized data
section .bss
String resb 128
N resb 1
section .text
global _start:
_start:
;Print message
mov eax, 4        ;sys_write
mov ebx, 1        ;stdout
mov ecx, msg1     ;message to write
mov edx, msg1_L   ;message length
int 80h
;input message and save
mov eax, 3 
mov ebx, 0 
mov ecx, String 
mov edx, 256 
int 80h 
;Copy 5th character to N, using base addressing mode
;This is where my problem is
mov bx, [String+4]
mov [N], bx
mov eax, 4      ;sys_write
mov ebx, 1      ;stdout
mov ecx, N      ;message to write
mov edx, 1      ;message length
int 80h
;Print new line
mov eax, 4        ;sys_write
mov ebx, 1        ;stdout
mov ecx, n_line   ;message to write
mov edx, 1        ;message length
int 80h
;This is where my problem is
mov bx, [String+4]
mov [N], bx

显然,String中只需要ASCII字符,因为您用N resb 1保留了1个字节。您应该使用一个字节寄存器将String的第5个字节复制到变量N。使用blal而不是bx

或者,如果您坚持使用寄存器寻址模式,您可以在32位程序中使用任何GPR,例如

mov esi,String   ; Put address og String to esi.
mov ebx,4        ; Put index of 5th byte to ebx.
mov al,[esi+ebx] ; Load 5th byte to al.
mov [N],al       ; Store al to the variable.

其他小问题:

您为String保留了128个字节,但要求sys_read最多读取256个字节。

Linux中不使用以美元结尾的n_line DB 0AH,0DH,"$"字符串,0AH足以制作一行新行。

您应该让您的程序以sys_exit优雅地终止。

最新更新