0

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
Sep Roland
  • 33,889
  • 7
  • 43
  • 76

1 Answers1

2
;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.

vitsoft
  • 5,515
  • 1
  • 18
  • 31
  • 2
    There aren't standard terms for x86 addressing modes (*[Do terms like direct/indirect addressing mode actual exists in the Intel x86 manuals](https://stackoverflow.com/q/46257018)*), but I'd guess "based" would most closely describe `[esi + 4]`, which has a base and displacement, not `[reg+reg]` being base+index. (Someone would probably describe `mov esi, String+4` / `movzx eax, byte [esi]` as register-indirect, although it's the addressing mode with only a base.) Anyway, *[Referencing the contents of a memory location. (x86 addressing modes)](//stackoverflow.com/q/34058101)* covers them all – Peter Cordes Sep 20 '22 at 20:40