I'm new to Assembly and learning the basics, but i've been stuck for a while on this and don't know how to get past it. The code below works, but not using the required base addressing mode.
I have to copy the fifth character from String into the variable N, using the base addressing mode. The way I went about it (not using base addressing mode) is by using base with offset. I'm not sure how I would achieve this doing base addressing mode, any help would be appreciated.
;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
Obviously only ASCII characters in the String
are expected, because you reserved 1 byte with N resb 1
. You should use one-byte register to copy 5th byte of String to the variable N
. Use bl
or al
instead of bx
.
Or, if you insist on employing register-addressing mode, you can use any GPR in 32bit programs, for instance
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.
Other minor issues:
You have reserved 128 bytes fo String
but you asked sys_read
to read up to 256 bytes.
n_line DB 0AH,0DH,"$"
Dollar-terminated strings are not used in Linux, 0AH
is enough to make a new line.
You should let your program terminate gracefully with sys_exit
.