5.2 通用代码,数组求和,拷贝数组,si配合di翻转数组
1. 通用代码
- 通用代码类似于一个用汇编语言写程序的一个框架,也类似于c语言的头文件编写
assume cs:code,ds:data,ss:stack
data segmentdata endsstack segmentstack endscode segmentstart:mov ax,datamov ds,axmov ax,4c00hint 21hcode ends
end start
2. 数组求和
assume cs:code,ds:data,ss:stack
data segmentarr db 1,2,3,4,10,20,30,40db 'hello world'
data endsstack segmentdb 100 dup (0)
stack endscode segmentstart:mov ax,datamov ds,axmov ax,0mov bx,0mov cx,8for:add al,ds:arr[bx]adc ah,0inc bxloop formov ax,4c00hint 21hcode ends
end startcomment*
vector<int>arr={1,2,3,4,10,20,30,40};
int sum = 0;
for (int = 0;i < arr.size();i++)sum += arr[i];
*comment
3. 拷贝数组
assume cs:code,ds:data,ss:stack
data segmentarr db 1,2,3,4,10,20,30,40res db 8 dup (0)
data endsstack segmentdb 100 dup (0)
stack endscode segmentstart:mov ax,datamov ds,axmov bx,0mov cx,8for:mov al,ds:arr[bx]mov ds:res[bx],alinc bxloop formov ax,4c00hint 21hcode ends
end startcomment*
vector<int>arr={1,2,3,4,10,20,30,40};
int res[8]
for (int i = 0;i < 8;i++)res[i] = arr [i];
*comment
4. 反转数组
assume cs:code,ds:data,ss:stack
data segmentarr db 1,2,3,4,10,20,30,40res db 8 dup (0)
data endsstack segmentdb 100 dup (0)
stack endscode segmentstart:mov ax,datamov ds,axmov si,0mov di,7mov cx,8for:mov al,arr[si]mov ds:res[di],alinc sidec diloop formov ax,4c00hint 21hcode ends
end startcomment*
vector<int>arr={1,2,3,4,10,20,30,40};
int res[8]
for (int i = 0;i < 8;i++)res[7-i] = arr [i];
for (int i = 0,j=7;i<8;i++,j--)res[j] = arr[i];
*comment