Assembly language programs examples


Question NO# 2
Write a program that is menu driven. The program takes an input from the user until it input’ % ‘Sign.
After getting input from the user the program displays a menu. Giving following options:
1) Converts the input to upper case. [2.5]
2) Search the char. Example: “Hello” user input “e “Output e is found at index ‘1’if char is found 2
time all the indexes are shown. [5]
3) Calculate the total occurrence of the letter. [5]
4) Bonus part sort the string. [5]
5) Reverse the array. [2.5]

Member 14691692 16-Dec-19 14:45
I note that you use XCHG quite frequently .. unfortunately there is a hidden "penalty" in the use of XCHG . The sort of error that one would expect from someone unfamiliar with assembler. Perhaps you would care to look at the definition and think again. Heres a clue .. you can use XCHG in a crude [but definitely not advisable way ] to halt a threading process .. why might this be . its ALL in the description ..
Sign in· View Thread
Member 14170884 4-Mar-19 10:13
; Clicking button saves & builds using commands: ; nasm -f elf -g -F stabs evil.asm ; ld -o evil evil.o section .data Snippet: db "@E9>06G@Q:CN3C57I <)<)*"SnipLen: equ $-Snippet section .text global _start _start: nop mov ecx,Snippet mov edx,SnipLen mov eax,6 DoMore: add byte [ecx],af inc ecx inc eax dec edx jnz DoMore mov eax,4 mov ebx,1 sub ecx,SnipLen mov edx,SnipLen int 80H mov eax,1 mov ebx,0 int 80H nop
Member 14784282 30-Apr-20 20:10

G3ZHX 31-Jan-19 8:25

mov eax,array ; eax =1
xchg eax,[array+4] ; 1,1,3, eax =2
xchg eax,[array+8] ; 1,1,2, eax =3
xchg array,eax ; 3,1,2, eax =1

The first and last lines should read

mov eax,[array+0] ; eax =1

xchg eax,[array+0] ; 3,1,2, eax =1 (or use mov [array+0],eax as indicated in the article)

Reason: Making it clear to anyone maintaining your code that these instructions are operating on an element of the array. In general all hard-coded accesses to the zeroth element of an array should take that form. A good assembler will generate the same code as your text.

I am still reading your article so apologies if you have already covered this further down the page.

Afterthought: I am very rusty with MASM, but doesn't the [] idiom act as addition? The clearer form of [foo+2] would then be foo[2], much more representative of a subscript in many computer languages.

Zuoliu Ding 2-Feb-19 7:54

Thanks for your question. Yes, [foo+2] is the same as foo[2]

This is called "Direct-Offset Operands"

A constant offset is added to a data label to produce an effective address (EA). The address is dereferenced to get the value inside its memory location.

where the square brackets are optional:

.data
arrayB BYTE 10h,20h,30h,40h
.code
mov al,arrayB+1 ; AL = 20h
mov al,[arrayB+1] ; alternative notation

So the example also can be like:

mov eax,array ; eax =1
xchg eax,array[4] ; 1,1,3, eax =2
xchg eax,array+8 ; 1,1,2, eax =3
xchg array,eax ; 3,1,2, eax =1

The format could be more flexible like:
xchg eax,8[array] ; 1,1,2, eax =3
xchg eax,8+array ; 1,1,2, eax =3
xchg eax,[8+array] ; 1,1,2, eax =3

All these are equivalent to array+8 or array[8]