Write an assembly language program to perform the following calculations: value1
ID: 2247036 • Letter: W
Question
Write an assembly language program to perform the following calculations:
value1 = a + b
value2 = a - b
value3 = a * b
value4 = value1 + value2 + value3
All data values (a and b) are word size. Allocate memory storage of appropriate sizes for value1, value2, value3, and value4. Test you program using the following data set (all values are in hex):
Data Set #
a
b
1
1
1
2
45
2
3
1111
2222
4
2222
1111
5
2
45
6
FFFF
25
7
0
65
8
1234
4321
9
4321
1234
10
ABCD
FF
11
FFFF
FFFF
Data Set #
a
b
1
1
1
2
45
2
3
1111
2222
4
2222
1111
5
2
45
6
FFFF
25
7
0
65
8
1234
4321
9
4321
1234
10
ABCD
FF
11
FFFF
FFFF
Explanation / Answer
class arcane_mystery {
private: long dark_secrets;
};
return sizeof(arcane_mystery);
return *(long *)&foo;
return *reinterpret_cast<long *>(&foo);
mov rdi, daString ; pointer to string
mov BYTE [rdi+0], 'Y'; change the string's bytes
extern puts
call puts ; print the string
ret
section .data ; switch storage mode to modifiable data
daString:
db `No.`,0 ;
int len=3;
int *arr=(int *)malloc(len*sizeof(int));
int i;
for (i=0;i<len;i++)
arr[i]=7+i;
iarray_print(arr,len);
free(arr);
mov rdi,8 ; a byte count to allocate
extern malloc
call malloc
; rax is start of our array
mov QWORD[rax],3 ; yay writeable memory!
mov rdi,rax ; pointer to deallocate
extern free
call free
ret
push rbx ; save register
mov rbx,3 ; number of integers (4-byte DWORDs) to allocate
mov rdi,rbx ; integer count
imul rdi,4 ; now a byte count
extern malloc
call malloc
; rax is start of our array
mov rcx,0 ; loop index
jmp loopTest
loopStart:
mov rdx,7
add rdx,rcx ; ==7+i
mov DWORD[rax+4*rcx],edx ; arr[i]=7+i (int version)
add rcx,1
loopTest:
cmp rcx,rbx ; i<len
jl loopStart
mov rdi,rax ; pointer to start of array
mov rsi,rbx ; number of integers
extern iarray_print
push rax ; save pointer
call iarray_print
pop rax ; restore pointer
mov rdi,rax; pointer to memory to free
extern free
call free
pop rbx ; restore register
ret
00000000 .DATA
00000000 EE FF byte0 BYTE 0EEh, 0FFh
00000002 1234 word2 WORD 1234h
00000004 56789ABC var4 DWORD 56789ABCh
00000008 00000000 var8 DWORD 0
00000000 .CODE
00000000 _start:
00000000 B8 00000002 R mov eax, OFFSET word2
00000005 A3 00000008 R mov [var8], eax
0000000A C3 ret ; Exit program
C:>DUMPBIN /DISASM little_endian.exe
Dump of file E:little_endian.exe
File Type: EXECUTABLE IMAGE
__start:
00301000: B8 02 40 30 00 mov eax,304002h
00301005: A3 08 40 30 00 mov dword ptr ds:[00304008h],eax
0030100A: C3 ret
message DB 'B'
DB 'y'
DB 'e'
DB 0DH
DB 0AH
can be written as
message DB 'B', 'y', 'e', 0DH, 0AH
and even more compactly as
message DB 'Bye', 0DH, 0AH
; label memory
; name offset
.DATA ; -------- -------
value DW 0 ; value 0
sum DD 0 ; sum 2
marks DW 10 DUP (?) ; marks 6
message DB 'The grade is:',0 ; message 26
char1 DB ? ; char1 40
; Immediate value moves
mov ax, 7 ; Immediate to register
mov mem, 7 ; Immediate to memory direct
mov mem[bx], 7 ; Immediate to memory indirect
; Register moves
mov mem, ax ; Register to memory direct
mov mem[bx], ax ; Register to memory indirect
mov ax, bx ; Register to register
mov ds, ax ; General register to segment register
; Direct memory moves
mov ax, mem ; Memory direct to register
mov ds, mem ; Memory to segment register
; Indirect memory moves
mov ax, mem[bx] ; Memory indirect to register
mov ds, mem[bx] ; Memory indirect to segment register
; Segment register moves
mov mem, ds ; Segment register to memory
mov mem[bx], ds ; Segment register to memory indirect
mov ax, ds ; Segment register to general register
; Move immediate to segment register
mov ax, DGROUP ; Load AX with immediate value
mov ds, ax ; Copy AX to segment register
; Move memory to memory
mov ax, mem1 ; Load AX with memory value
mov mem2, ax ; Copy AX to other memory
; Move segment register to segment register
mov ax, ds ; Load AX with segment register
mov es, ax ; Copy AX to segment register
xchg ax, bx ; exchange 16-bit regs
xchg ah, al ; exchange 8-bit regs
xchg eax, ebx ; exchange 32-bit regs
xchg [response], cl ; exchange 8-bit mem op with CL
xchg [total], edx ; exchange 32-bit mem op with EDX
.DATA
val1 WORD 1000h
val2 WORD 2000h
.CODE
mov ax, [val1] ; AX = 1000h
xchg ax, [val2] ; AX = 2000h, val2 = 1000h
mov [val1], ax ; val1 = 2000h
Consider:
.DATA
mem8 SBYTE -5
mem16 SWORD +5
mem32 SDWORD -5
.CODE
.
.
.
mov al, mem8 ; Load 8-bit -5 (FBh)
cbw ; Convert to 16-bit -5 (FFFBh) in AX
mov ax, mem16 ; Load 16-bit +5
cwd ; Convert to 32-bit +5 (0000:0005h) in DX:AX
mov ax, mem16 ; Load 16-bit +5
cwde ; Convert to 32-bit +5 (00000005h) in EAX
mov eax, mem32 ; Load 32-bit -5 (FFFFFFFBh)
cdq ; Convert to 64-bit -5
; (FFFFFFFF:FFFFFFFBh) in EDX:EAX
.DATA
mem8 BYTE 251
mem16 WORD 251
.CODE
.
.
.
mov al, mem8 ; Load 251 (FBh) from 8-bit memory
sub ah, ah ; Zero upper half (AH)
mov ax, mem16 ; Load 251 (FBh) from 16-bit memory
sub dx, dx ; Zero upper half (DX)
sub eax, eax ; Zero entire extended register (EAX)
mov ax, mem16 ; Load 251 (FBh) from 16-bit memory
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.